安卓横竖屏切换改变布局文件1

本文介绍了如何在安卓应用中实现横竖屏切换时自动改变布局文件。在AndroidManifest.xml中添加配置,根据不同的配置项,横竖屏切换会触发不同生命周期方法。新建横屏和竖屏布局文件,并在Java代码中处理布局切换,以确保正确显示。
摘要由CSDN通过智能技术生成

安卓横竖屏切换改变布局文件1

开启安卓的自动旋转,在改变方向后,自动更改布局文件,其中activity会重启,数据不会保存,需要后面进行数据保存恢复

在AndroidManifest添加

android:configChanges="keyboardHidden|screenSize|orientation"
正常横竖屏切换的生命周期:
onPause()->onSaveInstanceState()-> onStop()->onDestroy()->onCreate()->onStart()->onRestoreInstanceState->onResume()

  1. 不设置Activity的android:configChanges时,切屏会重新调用各个生命周期,切横屏时会执行一次,切竖屏时会执行两次
  2. 设置Activity的android:configChanges="orientation"时,切屏还是会重新调用各个生命周期,切横、竖屏时只会执行一次
  3. 设置Activity的android:configChanges="orientation|keyboardHidden"时,切屏不会重新调用各个生命周期,只会执行onConfigurationChanged方法

新建横屏布局

  1. 切换到Project

在这里插入图片描述
2.在res目录下新建
在这里插入图片描述
2. 选择layout->Orientation在这里插入图片描述
3. 选择横屏在这里插入图片描述
4.然后将原布局复制或新建同名文件在layout,此时就会出现两个布局文件
在这里插入图片描述
5. 初步能实现布局的切换,但是文件的布局不同时会出现显示不正确,此时我们需要在Java文件中添加

@Override
    public void onConfigurationChanged(Configuration newConfig) {
   
        super.onConfigurationChanged(newConfig);
        setContentView(R.layout.activity_main);
    }

竖屏布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="bottom">

    <EditText
        android:id="@+id/et_input"
        android:layout_width="match_parent"
        android:gravity="end"
        android:layout_height="60dp"
        android:focusableInTouchMode="false"
        android:hint="0"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_marginTop="5dp"
        android:gravity="center_horizontal"
        android:orientation="horizontal">

        <Button
            android:id="@+id/clear"
            android:layout_width="80dp"
            android:layout_height="60dp"
            android:textSize="20sp"
            android:text="c"/>
        <Button
            android:id="@+id/del"
            android:layout_width="80dp"
            android:layout_height="60dp"
            android:textSize="20sp"
            android:layout_marginLeft="5dp"
            android:text="del"/>
        <Button
            android:id="@+id/division"
            android:layout_width="80dp"
            android:layout_height="60dp"
            android:textSize="20sp"
            android:layout_marginLeft="5dp"
            android:text="/"/>
        <Button
            android:id="@+id/mult"
            android:layout_width="80dp"
            android:layout_height="60dp"
            android:textSize="20sp"
            android:layout_marginLeft="5dp"
            android:text="*"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_marginTop="5dp"
        android:gravity="center_horizontal"
        android:orientation="horizontal">

        <Button
            android:id="@+id/bt1"
            android:layout_width="80dp"
            android:layout_height="60dp"
            android:textSize="20sp"
            android:text="1"/>
        <Button
            android:id="@+id/bt2"
            android:layout_width="80dp"
            android:layout_height="60dp"
            android:textSize="20sp"
            android:layout_marginLeft="5dp"
            android:text="2"/>
        <Button
            android:id="@+id/bt3"
            android:layout_width="80dp"
            android:layout_height="60dp"
            android:textSize="20sp"
            android:layout_marginLeft="5dp"
            android:text="3"/>
        <Button
            android:id="@+id/sub"
            android:layout_width="80dp"
            android:layout_height="60dp"
            android:textSize="20sp"
            android:layout_marginLeft="5dp"
            android:text="-"/>
    </LinearLayout
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值