【问题标题】:android incorrect layout for tablet in setContentViewandroid setContentView 中平板电脑的布局不正确
【发布时间】:2025-11-22 18:45:02
【问题描述】:

我正在开发一个玩具应用程序,它具有不同的手机和平板电脑布局文件,但在平板电脑模式下,它没有使用 layout-sw600dp 文件夹中的正确布局。这是我的文件内容:

activity_main.xmllayout-sw600dp 文件夹中:

<LinearLayout 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:baselineAligned="false"
    android:divider="?android:attr/dividerHorizontal"
    android:orientation="horizontal"
    tools:context="com.example.android.sunshine.app.MainActivity">

    <!--
    This layout is a two-pane layout for the Items master/detail flow.
    -->

    <fragment
        android:id="@+id/fragment_forecast"
        android:name="com.example.android.sunshine.app.ForecastFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2"
        tools:layout="@android:layout/list_content" />

    <FrameLayout
        android:id="@+id/weather_detail_container"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="4" />

</LinearLayout> 

activity_main.xmllayout 文件夹中:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fragment_forecast"
    android:name="com.example.android.sunshine.app.ForecastFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    tools:context="com.example.android.sunshine.app.ForecastFragment"
    tools:layout="@android:layout/list_content" />

MainActivity.java 中的onCreate 方法:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mLocation = Utility.getPreferredLocation(this);
    setContentView(R.layout.activity_main);

    if(findViewById(R.id.weather_detail_container) != null)
    {
        mTwoPane = true;

        if(savedInstanceState == null)
        {
            getSupportFragmentManager().beginTransaction().replace(R.id.weather_detail_container, new DetailActivityFragment(),DETAILFRAGMENT_TAG).commit();
        }
    }
    else
    {
        mTwoPane = false;
    }
}

我正在使用emulated Nexus 10 来测试应用程序。当应用程序出现时,它会显示单窗格布局,与手机视图相同。我不知道这里出了什么问题。

我也尝试了emulated Nexus 7,但问题仍然存在。

令人惊讶的是,我尝试使用 Samsung Galaxy Note 10.1 API 16 real device 并且布局完美。导致问题的仿真设备是否存在任何问题?

编辑

我怀疑模拟设备有问题并尝试emulate Samsung Galaxy Note 10.1 API 16。布局也适用于设备的模拟版本。

我也尝试将API 16emulated Nexus 7 一起使用,但我仍然得到了单窗格布局。 Nexus 7 &amp; 10layout-sw600dp 文件夹有问题吗?

【问题讨论】:

  • 你试过sw720dp吗?
  • Nexus 10 使用 layout-sw720dp 文件夹。 Here 更多详情。
  • @gj_ 是的,我试过了,同样的问题
  • @VladimirJovanović 顾名思义,它是最小的宽度,并且由于 nexus 10 属于该类别,它应该显示它。无论如何,我尝试使用 sw720 并没有运气

标签: android android-layout


【解决方案1】:

是的,这是模拟器的问题。

直接解决方案 - 不要使用 Nexus 皮肤。

我测试了各种配置:Nexus 9 应该可以正常工作。

在论坛的几乎所有 Android 课程(主题:片段,两窗格模式)中,您都可以找到有关此问题的投诉。

SO 问题:12

android 问题跟踪器中有一些问题:12

这个问题很老了,没有官方回应。

【讨论】:

  • 你能分享一些参考资料来证明这一点吗?
  • 有同样的问题...经验...你可以自己测试一下。
  • 如果你阅读了我的“编辑”部分,我提到了一些设备可以工作,而另一些设备则不能,我实际上是在寻找原因
  • 您是否尝试过相同的配置但没有 Nexus 皮肤?
  • 我不记得了,因为这是很久以前的事了,但会测试并告诉你
最近更新 更多