【问题标题】:Android XML layout change on rotation not working properly旋转时的 Android XML 布局更改无法正常工作
【发布时间】:2021-06-02 23:32:44
【问题描述】:

问题

XML 布局在我的设备旋转时发生变化。

如果我旋转到横向模式,布局会发生变化,但如果我想旋转到纵向,它会再次加载横向模式。


我尝试过的事情

我尝试通过添加 android:configChanges="keyboardHidden|orientation|screenSize"> 在清单中,但这并没有解决我的问题。

我尝试重新创建 XML 布局的横向版本,但仍然没有解决问题。


XML 和清单

清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.neuroinit">
    
    <uses-feature android:name="android.hardware.camera"
        android:required="true" />
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />


    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:networkSecurityConfig="@xml/network_security_config">

        <uses-library
            android:name="org.apache.http.legacy"
            android:required="false" />
        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="@string/google_map_api_key" />
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

        <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="com.example.android.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths"/>
        </provider>

        <activity android:name=".MainActivity"
            android:configChanges="keyboardHidden|orientation|screenSize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

肖像

<?xml version="1.0" encoding="utf-8"?>
<layout 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"
    tools:context=".angle.AngleFragment">

    <data>
        <variable
            name="angleViewModel"
            type="com.example.neuroinit.angle.AngleViewModel" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/angleLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/mapImageView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:contentDescription="@string/mapview_of_countingpoint"
            android:scaleType="center"
            android:src="@drawable/ic_launcher_foreground"
            app:countingImage="@{angleViewModel.counting}"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <com.example.neuroinit.angle.LineView
            android:id="@+id/test"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:text="@string/clicked_angle_0"
            app:layout_constraintBottom_toTopOf="@+id/angleView"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/mapImageView"
            style="@style/TextAppearance.MaterialComponents.Headline4"/>

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/angleView"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginTop="16dp"
       app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView3">

        </androidx.recyclerview.widget.RecyclerView>

    </androidx.constraintlayout.widget.ConstraintLayout>

</layout>

风景

<?xml version="1.0" encoding="utf-8"?>
<layout 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"
    tools:context=".angle.AngleFragment">

    <data>
        <variable
            name="angleViewModel"
            type="com.example.neuroinit.angle.AngleViewModel" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/angleLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/mapImageView"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:contentDescription="@string/mapview_of_countingpoint"
            android:scaleType="center"
            android:src="@drawable/ic_launcher_foreground"
            app:countingImage="@{angleViewModel.counting}"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/angleView"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <com.example.neuroinit.angle.LineView
            android:id="@+id/test"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/clicked_angle_0"
            android:layout_marginTop="16dp"
            app:layout_constraintBottom_toTopOf="@+id/angleView"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toEndOf="@+id/mapImageView"
            app:layout_constraintTop_toTopOf="parent"
            style="@style/TextAppearance.MaterialComponents.Headline4"/>

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/angleView"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginTop="16dp"
         app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toEndOf="@+id/mapImageView"
            app:layout_constraintTop_toBottomOf="@+id/textView3">

        </androidx.recyclerview.widget.RecyclerView>

    </androidx.constraintlayout.widget.ConstraintLayout>

</layout>

我知道有点乱 ;D


旁注

奇怪的是,我有另一个包含纵向和横向 XML 的片段,它的作用就像一个魅力。

【问题讨论】:

  • 欢迎来到 StackOverflow,最好分享您的 XML 布局和清单文件。
  • 刚刚更新了我的问题希望对您有所帮助:)
  • 澄清一下,您是否尝试过不设置任何configChanges 标志?这些真的不应该使用,除非你有一个非常具体的原因,它更多地讨论了为什么在这里developer.android.com/guide/topics/resources/…
  • 是的,我一开始没有使用标志。我刚刚读到这个标志可能会有所帮助。在它对我不起作用后我将其删除。

标签: android android-layout kotlin


【解决方案1】:

我对您的问题有所了解。试试这些,让我知道结果。

  • 第一个选项

onCreate(){
   setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
}

onPause(){ 
  if (android.os.Build.VERSION.SDK_INT >= 27) {
         setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
  }
}

onResume(){
  if (android.os.Build.VERSION.SDK_INT >= 27) {
         setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
  }
}
  • 第二个选项

重写 Landscape 活动的 onBackPressed() 方法并将方向设置为 Portrait。

@Override
public void onBackPressed() {
    super.onBackPressed();
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}

【讨论】:

  • 我尝试了第一个选项,但它对我不起作用。仍然没有从旋转中改变布局。而第二个选项我不明白这将如何解决旋转问题 D:
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-17
  • 2012-10-10
相关资源
最近更新 更多