android - 更改方向时 fragment 不使用横向布局

标签 android android-fragments android-orientation

我通过单击设计预览上方的创建横向版本按钮创建了该 fragment 的横向版本。然后我对横向布局文件做了一些修改。但是当方向改变时, fragment 不会使用该 fragment 的横向版本。

在 list 中,我将 android:configChanges="orientation|keyboardHidden|screenSize" 添加到包含该 fragment 的 Activity 中。

但这没有用。

肖像版

fragment_feed_customization.xml

<RelativeLayout xmlns: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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#ffffff"
tools:context="com.shrikanthravi.newslly.FeedCustomizationFragment">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:layout_marginTop="-45dp"
    android:paddingTop="55dp"
    android:clipToPadding="false"
    android:layout_below="@+id/ll"
    android:id="@+id/CategoryRV">

</android.support.v7.widget.RecyclerView>
<android.support.v7.widget.CardView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    app:cardElevation="10dp"
    app:cardCornerRadius="20dp"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="15dp">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:text="Update"
        android:visibility="gone"
        android:textAllCaps="false"
        android:textColor="@android:color/white"
        android:id="@+id/UpdateButton"
        android:background="@color/color2"/>
</android.support.v7.widget.CardView>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/ll"
        android:background="@drawable/personalize_bg_gradient"
        android:orientation="vertical">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:textColor="@color/color2"
            android:textSize="23sp"
            android:text="Personalize your feed"
            android:layout_marginTop="10dp"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="8dp"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="Pick at least 3 topics of your interest"/>

    </LinearLayout>

</RelativeLayout>

横向版

fragment_feed_customization.xml

<RelativeLayout xmlns: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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#ffffff"
tools:context="com.shrikanthravi.newslly.FeedCustomizationFragment">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:layout_marginTop="-45dp"
    android:paddingTop="55dp"
    android:clipToPadding="false"
    android:layout_below="@+id/ll"
    android:id="@+id/CategoryRV">

</android.support.v7.widget.RecyclerView>
<android.support.v7.widget.CardView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    app:cardElevation="10dp"
    app:cardCornerRadius="20dp"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="15dp">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:text="Update"
        android:visibility="gone"
        android:textAllCaps="false"
        android:textColor="@android:color/white"
        android:id="@+id/UpdateButton"
        android:background="@color/color2"/>
</android.support.v7.widget.CardView>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/ll"
        android:weightSum="2"
        android:background="@drawable/personalize_bg_gradient"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:textColor="@color/color2"
            android:layout_weight="1"
            android:textSize="20dp"
            android:text="Personalize your feed : "
            android:layout_margin="10dp"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:layout_weight="1"
            android:textSize="20dp"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="Pick at least 3 topics of your interest"/>

    </LinearLayout>

</RelativeLayout>

最佳答案

只需删除 android:configChanges="orientation|keyboardHidden|screenSize"

防止覆盖横向布局

关于android - 更改方向时 fragment 不使用横向布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50663821/

相关文章:

android - OkHttp - Android 从 MySQL 数据库获取数据

android - 无法在 Android Studio 1.3 中将 gradle 插件更新为 1.3.0-beta4

android - 每次在 Activity 中获取 savedInstanceState null

java - 无法从 fragment 类在 Textview 上设置文本

java - 停止 fragment 的函数和调用并重置内部变量

android - 如何在运行时将 fragment 的屏幕方向锁定为横向

android - Android 和 iOS 上的 HTML5 音频有哪些限制?

android - list 中 facebook 应用程序的应用程序 ID 错误

android - android 中三星设备上的相机捕获方向

android - 快速旋转手机180度,相机预览倒转