189 8069 5689

横竖屏切换显示不同布局

在项目开发中,

成都网站建设、网站制作的关注点不是能为您做些什么网站,而是怎么做网站,有没有做好网站,给创新互联建站一个展示的机会来证明自己,这并不会花费您太多时间,或许会给您带来新的灵感和惊喜。面向用户友好,注重用户体验,一切以用户为中心。

关于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

"1.0"encoding="utf-8"?>

"http://schemas.android.com/apk/res/android"

   android:layout_width="match_parent"

   android:layout_height="match_parent"

   android:orientation="vertical">

 

   

       android:id="@+id/button1_landscape"

       android:layout_width="fill_parent"

       android:layout_height="wrap_content"

       android:text="LandScape"/>

 

   

       android:id="@+id/textView1"

       android:layout_width="fill_parent"

       android:layout_height="wrap_content"

       android:gravity="center"

       android:text="横屏界面"

       android:textAppearance="?android:attr/textAppearanceLarge"/>

 

 

//portrait_screen.xml

"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">

 

   

       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"/>

 

   

       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"/>

 

 

//manifest.xml

"1.0"encoding="utf-8"?>

"http://schemas.android.com/apk/res/android"

   package="com.example.testscreen"

   android:versionCode="1"

   android:versionName="1.0">

 

   

       android:minSdkVersion="8"

       android:targetSdkVersion="18"/>

 

   

       android:allowBackup="true"

       android:icon="@drawable/ic_launcher"

       android:label="@string/app_name"

       android:theme="@style/AppTheme"

       >

       

           android:name="com.example.testscreen.ScreenActivity"

           android:label="@string/app_name"

          

           android:configChanges="keyboardHidden|orientation|screenSize">

           

               "android.intent.action.MAIN"/>

 

               "android.intent.category.LAUNCHER"/>

           

       

   

 


文章标题:横竖屏切换显示不同布局
本文路径:http://cdxtjz.cn/article/ihihgj.html

其他资讯