【发布时间】: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/360211 或 this 等应用程序
-
@CommonsWare 我添加了加载每个布局的代码。我认为这是正确的代码。
标签: java android xml android-layout