【问题标题】:Android "values-large-land" resource not loadingAndroid“values-large-land”资源未加载
【发布时间】:2015-08-17 18:40:34
【问题描述】:

我发现的任何与此相关的问题总是通过更正布局文件路径来解决,我很确定我的问题是正确的。

我做了一个非常简单的布局,应该为大型横向设备提供拆分视图。所以我有:

  • 布局/main.xml
  • 布局/main_twopane.xml
  • values/main.xml
  • values-large-land/main.xml

我正在横向加载 Nexus 7 平板电脑(被归类为大型设备)。问题是它总是加载main.xml,而根据Android documentation,我应该在横向上为大型设备加载两个窗格布局。

代码如下:

布局/main.xml

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

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fragment_catches"
    android:name="com.cohenadair.anglerslog.fragments.CatchesFragment"
    tools:layout="@layout/fragment_catches"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</LinearLayout>

布局/main_twopane.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/fragment_catches"
        android:name="com.cohenadair.anglerslog.fragments.CatchesFragment"
        tools:layout="@layout/fragment_catches"
        android:layout_width="200dp"
        android:layout_height="match_parent" />

    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/fragment_catch"
        android:name="com.cohenadair.anglerslog.fragments.CatchFragment"
        tools:layout="@layout/fragment_catch"
        android:layout_width="fill_parent"
        android:layout_height="match_parent" />

</LinearLayout>

值/main.xml

<resources>
    <item name="main_layout" type="layout">@layout/main</item>
    <bool name="has_two_panes">false</bool>
</resources>

values-large-land/main.xml

<resources>
    <item name="main_layout" type="layout">@layout/main_twopane</item>
    <bool name="has_two_panes">true</bool>
</resources>

加载布局的代码,在MainActivity.java:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}

在每个单独的片段中:

@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View aView = super.onCreateView(inflater, container, savedInstanceState);

    // set the list's adapter
    ArrayAdapter adapter = new ArrayAdapter<Catch>(this.getActivity(), android.R.layout.simple_list_item_1, Logbook.getSharedLogbook().getCatches());
    setListAdapter(adapter);

    return aView;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_catch, container, false);
}

【问题讨论】:

  • 请将您的 Java 代码发布到您正在加载此布局的位置。
  • 嗯?我的印象是系统会根据设备大小/方向自动加载布局。问题中的文档链接没有提及有关加载布局的任何内容(如果有,我错过了)。
  • “我的印象是系统会根据设备大小/方向自动加载布局”——仅响应某处尝试加载布局的一些 Java 代码,例如 @987654333 @ 在Activity 上,或inflate()LayoutInflater 上。
  • Nexus 7 平板电脑 - 我相信这有问题,它认为它是具有特定更新版本的手机(尺寸、电话、工作)。请使用此代码进行验证:stackoverflow.com/a/5016350/360211this 等应用程序
  • @CommonsWare 我添加了加载每个布局的代码。我认为这是正确的代码。

标签: java android xml android-layout


【解决方案1】:

问题是它总是加载 main.xml

那是因为你告诉 Android 加载 main:

setContentView(R.layout.main);

如果您希望它加载 main_layout,正如您的 values/... 内容所建议的那样,请将其更改为:

setContentView(R.layout.main_layout);

我不太确定您为什么要在这里使用布局资源别名方法。恕我直言,对于您来说拥有res/layout/main.xmlres/layout-large-land/main.xml 会更简单,然后只需在您的Java 代码中请求R.layout.main,然后转储别名。但是,您的别名应该可以工作,但当且仅当您请求别名时。

【讨论】:

  • 我正在使用别名,因为最终我将多次重用片段,并且我不想要重复的 layout.xml 文件。切换到R.layout.main_layout 似乎不起作用。我将更改设备,因为上面的评论提到了 Nexus 7 平板电脑中可能存在的错误。
  • 我认为这是你的问题,无论哪种方式,都不需要切换设备,只需验证它实际上认为它很大。
  • 这就是解决方案。谢谢,CommonsWare。韦斯顿,你是对的,Android 确实认为 Nexus 7 模拟器是一部手机。谢谢你。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多