控制横竖屏显示不同界面

在项目开发中,

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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值