您好,登錄后才能下訂單哦!
在項目開發中,
關于android手機橫豎屏切換時顯示不同的界面,在這里我定義了兩個xml布局文件
landscape_screen.xml,portrait_screen.xml
根據屏幕的旋轉切換不同的布局文件
重寫onConfigurationChanged方法,對其進行監聽并判斷當前的屏幕狀態,根據其狀態顯示對應的布局文件
當然在manifest.xml中對應的activity中要加上
android:configChanges="keyboardHidden|orientation|screenSize">
貼上代碼說話
public classScreenActivity extends Activity implements OnClickListener{
private Button btn;
@Override
protected voidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//默認為豎屏
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"><!-- 這句話不能忘記,起初少了這一個|screenSize參數,一直出現錯誤 -->
<intent-filter>
<actionandroid:name="android.intent.action.MAIN"/>
<categoryandroid:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。