温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

横竖屏切换 显示不同布局

发布时间:2020-07-10 14:32:45 来源:网络 阅读:967 作者:671076656 栏目:移动开发

在项目开发中,

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

 


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI