gpt4 book ai didi

java - Android "values-large-land"资源未加载

转载 作者:行者123 更新时间:2023-11-29 20:30:20 25 4
gpt4 key购买 nike

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

我制作了一个非常简单的布局,应该为大型横向设备提供 Split View。所以我有:

  • layout/main.xml
  • layout/main_twopane.xml
  • values/main.xml
  • values-large-land/main.xml

我正在横向加载 Nexus 7 平板电脑(归类为大型设备)。问题是它总是根据 Android documentation 加载 main.xml我所拥有的应该为横向的大型设备加载两个 Pane 布局。

代码如下:

布局/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);
}

在每个单独的 fragment 中:

@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);
}

最佳答案

The problem is it is always loading 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,那么只需请求 R.layout.main 在您的 Java 代码中,并转储别名。但是,您的别名应该有效,但当且仅当您请求别名时。

关于java - Android "values-large-land"资源未加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32057447/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com