在項(xiàng)目開(kāi)發(fā)中,
創(chuàng)新互聯(lián)專注于高平企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站建設(shè),商城網(wǎng)站定制開(kāi)發(fā)。高平網(wǎng)站建設(shè)公司,為高平等地區(qū)提供建站服務(wù)。全流程按需開(kāi)發(fā),專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)關(guān)于android手機(jī)橫豎屏切換時(shí)顯示不同的界面,在這里我定義了兩個(gè)xml布局文件
landscape_screen.xml,portrait_screen.xml
根據(jù)屏幕的旋轉(zhuǎn)切換不同的布局文件
重寫(xiě)onConfigurationChanged方法,對(duì)其進(jìn)行監(jiān)聽(tīng)并判斷當(dāng)前的屏幕狀態(tài),根據(jù)其狀態(tài)顯示對(duì)應(yīng)的布局文件
當(dāng)然在manifest.xml中對(duì)應(yīng)的activity中要加上
android:configChanges="keyboardHidden|orientation|screenSize">
貼上代碼說(shuō)話
public classScreenActivity extends Activity implements OnClickListener{
private Button btn;
@Override
protected voidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//默認(rèn)為豎屏
setupViewInPortraitLayout();
}
//豎屏界面
private voidsetupViewInPortraitLayout(){
setContentView(R.layout.portrait_screen);
btn = (Button)findViewById(R.id.button1_portrait);
btn.setOnClickListener(this);
}
//橫屏界面
private voidsetupViewInLandscapeLayout(){
setContentView(R.layout.landscape_screen);
btn = (Button)findViewById(R.id.button1_landscape);
btn.setOnClickListener(this);
}
@Override
public voidonConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Toast.makeText(ScreenActivity.this,"onConfigurationChanged", Toast.LENGTH_LONG).show();
Configuration cfg =getResources().getConfiguration();
if (cfg.orientation ==Configuration.ORIENTATION_LANDSCAPE) {
setupViewInLandscapeLayout();
} else if(cfg.orientation == Configuration.ORIENTATION_PORTRAIT) {
setupViewInPortraitLayout();
}
}
@Override
public void onClick(Viewarg0) {
// TODO Auto-generatedmethod stub
if(arg0.equals(btn)){
Toast.makeText(ScreenActivity.this,"Click", Toast.LENGTH_LONG).show();
}
}
}
//landscape_screen.xml
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/button1_landscape"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="LandScape"/>
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="橫屏界面"
android:textAppearance="?android:attr/textAppearanceLarge"/>
</LinearLayout>
//portrait_screen.xml
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".ScreenActivity">
<Button
android:id="@+id/button1_portrait"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="Portrait"/>
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button1_portrait"
android:gravity="center"
android:text="豎屏界面"
android:textAppearance="?android:attr/textAppearanceLarge"/>
</RelativeLayout>
//manifest.xml
<?xmlversion="1.0"encoding="utf-8"?>
<manifestxmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testscreen"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
>
<activity
android:name="com.example.testscreen.ScreenActivity"
android:label="@string/app_name"
android:configChanges="keyboardHidden|orientation|screenSize"><!-- 這句話不能忘記,起初少了這一個(gè)|screenSize參數(shù),一直出現(xiàn)錯(cuò)誤 -->
<intent-filter>
<actionandroid:name="android.intent.action.MAIN"/>
<categoryandroid:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
網(wǎng)站標(biāo)題:橫豎屏切換顯示不同布局-創(chuàng)新互聯(lián)
文章來(lái)源:http://vcdvsql.cn/article16/jisdg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供手機(jī)網(wǎng)站建設(shè)、微信公眾號(hào)、網(wǎng)站制作、軟件開(kāi)發(fā)、域名注冊(cè)、動(dòng)態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容