【问题标题】:Android layout-land not working as expectedAndroid layout-land 没有按预期工作
【发布时间】:2018-03-07 04:03:17
【问题描述】:

我在 AndroidStudio 中遇到了 layout-land 的问题。

我有一个我的 layout 文件(同名)的副本,背景已更改,以查看 \res\layout-land\res 中的原始布局文件的区别\布局。我没有使用android:configChanges="orientation 在我的AndroidManifest.xml 或我的布局文件中。

我尝试在 Android Studio 的 Android 模拟器中定位 Nexus 5Pixel 2 (Oreo 8.0),并尝试在我的物理 ZTE ZFive 2 (Marshmallow 6.0.1)。

作为最后的努力,我尝试将原始布局文件移动到 \res\layout-port 并删除 \res\layout 目录以强制它使用 \res\layout-port\res\layout-land xml 文件。这导致在我的MainActivity.javaonCreate() 中的setContentView(R.layout.activity_main) 上切换到横向时出错,并且它给了我一个关于没有布局的错误。

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.kozlowlw.test">
    <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">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:background="@color/background" <!--Changed to fff from ccc-->
    tools:context="com.example.kozlowlw.test.MainActivity">

    <TableLayout
        android:id="@+id/table_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_margin="15dp"
        android:stretchColumns="*">

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <Button
                android:id="@+id/button0"
                android:layout_width="15dp"
                android:layout_height="wrap_content"
                android:textAlignment="center"
                android:textSize="20sp" />

            <Button
                android:id="@+id/button1"
                android:layout_width="15dp"
                android:layout_height="wrap_content"
                android:textAlignment="center"
                android:textSize="20sp" />

            <Button
                android:id="@+id/button2"
                android:layout_width="15dp"
                android:layout_height="wrap_content"
                android:textAlignment="center"
                android:textSize="20sp" />

            <Button
                android:id="@+id/button3"
                android:layout_width="15dp"
                android:layout_height="wrap_content"
                android:textAlignment="center"
                android:textSize="20sp" />

            <Button
                android:id="@+id/button4"
                android:layout_width="15dp"
                android:layout_height="wrap_content"
                android:textAlignment="center"
                android:textSize="20sp" />

            <Button
                android:id="@+id/button5"
                android:layout_width="15dp"
                android:layout_height="wrap_content"
                android:textAlignment="center"
                android:textSize="20sp" />

            <Button
                android:id="@+id/button6"
                android:layout_width="15dp"
                android:layout_height="wrap_content"
                android:textAlignment="center"
                android:textSize="20sp" />
        </TableRow>

    </TableLayout>

    <TextView
        android:id="@+id/game_state_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/table_layout"
        android:layout_centerHorizontal="true"
        android:text="@string/message_start"
        android:textAlignment="center"
        android:textSize="20sp" />

    <Button
        android:id="@+id/new_game_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignEnd="@+id/table_layout"
        android:layout_alignRight="@+id/table_layout"
        android:layout_below="@+id/table_layout"
        android:text="@string/new_game_button_text"
        android:textAlignment="center"
        android:textSize="20sp" />


</RelativeLayout>

文件夹布局

文件夹结构

【问题讨论】:

  • 它运作良好。
  • 那么我的安装有问题吗?
  • 不需要使用layout-port
  • 为了清楚起见,只需创建一个新项目并创建一个目录 layout-land 并将 activity_main.xml 从 layout 复制到 layout-land,只需将不同的颜色设置为背景。它会按您的预期工作。
  • 那只是为了检查它是否能够找到 layout-land,我已经删除了 layout-port。想如果我明确布局并删除默认布局文件,它将帮助我了解为什么 layout-land 失败(我发现是横向时的 Resources$NotFoundException)

标签: java android xml android-layout


【解决方案1】:

在构建失败后资源未被清除存在问题。对项目进行清理和重建(构建 -> 清理项目,构建 -> 重建项目)解决了我的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-21
    • 2019-01-08
    • 1970-01-01
    • 1970-01-01
    • 2021-10-19
    相关资源
    最近更新 更多