Android活动加载了错误的布局文件
我正在开发一个简单的应用程序,在这里你可以看到音乐家的列表,当你点击一个音乐家时,你会得到一些关于它的细节(流派、热门歌曲和传记)。 我想要实现的功能是:如果您在大屏幕上观看,请将音乐家列表放在左侧,详细信息应显示在右侧(单击音乐家后);如果是窄屏幕,则详细信息应单独显示在新屏幕上。功能应该通过片段和框架布局来完成 所以我有了Android活动加载了错误的布局文件,android,android-layout,android-fragments,Android,Android Layout,Android Fragments,我正在开发一个简单的应用程序,在这里你可以看到音乐家的列表,当你点击一个音乐家时,你会得到一些关于它的细节(流派、热门歌曲和传记)。 我想要实现的功能是:如果您在大屏幕上观看,请将音乐家列表放在左侧,详细信息应显示在右侧(单击音乐家后);如果是窄屏幕,则详细信息应单独显示在新屏幕上。功能应该通过片段和框架布局来完成 所以我有了activity\u main.xml,我去添加新的资源文件->布局文件,我添加了activity\u main.xml(w600dp),我希望它能自动加载到面向风景的智能
activity\u main.xml
,我去添加新的资源文件->布局文件,我添加了activity\u main.xml(w600dp)
,我希望它能自动加载到面向风景的智能手机或平板电脑上
活动\u main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/muzicari_lista"
android:layout_weight="1">
</FrameLayout>
</LinearLayout>
<?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">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/lista_muzicara"></FrameLayout>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/detalji_muzicar"></FrameLayout>
</LinearLayout>
w600dp/activity\u main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/muzicari_lista"
android:layout_weight="1">
</FrameLayout>
</LinearLayout>
<?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">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/lista_muzicara"></FrameLayout>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/detalji_muzicar"></FrameLayout>
</LinearLayout>
包含列表和详细信息的XML片段非常简单,只有几个文本块。
以下是mainActivity类的onCreate方法:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
unosi = new ArrayList<Muzicar>(); //let's presume there's something in this list
wideL = false;
FragmentManager fm = getFragmentManager();
//fetching FragmentManager
FrameLayout ldetalji = (FrameLayout)findViewById(R.id.detalji_muzicar);
if(ldetalji!=null){
//layout for wide screens
wideL=true;
FragmentDetalji fd;
fd = (FragmentDetalji)fm.findFragmentById(R.id.detalji_muzicar);
//checking if there is FragmentDetalji already created:
if(fd==null) {
//if not, we're creating it now
fd = new FragmentDetalji();
fm.beginTransaction().replace(R.id.detalji_muzicar, fd).commit();
}
}
FragmentLista fl = (FragmentLista)fm.findFragmentByTag("Lista");
//we're checking if there's already fl created
if(fl==null){
//if it hasn't been created:
fl = new FragmentLista();
Bundle argumenti = new Bundle();
argumenti.putParcelableArrayList("Alista",unosi);
fl.setArguments(argumenti);
fm.beginTransaction().replace(R.id.muzicari_lista, fl).commit();
}else{
//case when we change from portrait to landscape, and there was FragmentDetalji already open
fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
}
}
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
unosi=new ArrayList();//假设列表中有一些内容
wideL=假;
FragmentManager fm=getFragmentManager();
//获取碎片管理器
FrameLayout ldetalji=(FrameLayout)findViewById(R.id.detalji_muzicar);
if(ldetalji!=null){
//宽屏幕布局
wideL=真;
残缺的德塔尔吉;
fd=(FragmentDetalji)fm.findframentbyid(R.id.detalji_muzicar);
//检查是否已创建FragmentDetalji:
如果(fd==null){
//如果不是,我们现在正在创建它
fd=新片段detalji();
fm.beginTransaction().replace(R.id.detalji_muzicar,fd.commit();
}
}
FragmentLista fl=(FragmentLista)fm.findFragmentByTag(“Lista”);
//我们正在检查是否已经创建了fl
如果(fl==null){
//如果尚未创建:
fl=新碎片列表a();
Bundle argumenti=新Bundle();
辩论一.政界领袖候选人(“Alista”,unosi);
fl.setArguments(argumenti);
fm.beginTransaction().replace(R.id.muzicari_lista,fl.commit();
}否则{
//当我们从肖像画转向风景画的时候,有一个片段已经打开了
fm.popBackStack(null,FragmentManager.POP_BACK_STACK_,包括在内);
}
}
类FragmentDetalji
和FragmentLista
除了利用onCreateView
之外,没有什么特殊功能(尽管如果它们可能是问题的根源,我可以添加它们)
问题是,无论我使用什么加载此应用程序,activity_main都会以默认形式加载,而不是
w600dp
版本!我做错了什么?要提供替代布局资源,您需要在res文件夹中创建一个子目录,名为layout-sw600dp
,并将更改后的布局放在其中,其确切名称与其他小屏幕布局相同。然后,您的活动将自动查找此文件夹。因此,如果您的xml文件标题为activity\u main.xml
,那么它在layout-sw600dp
文件夹中也应该具有完全相同的名称
您还可以通过为大屏幕创建名为layout land
或layout-sw600dp-land
的子目录,为景观提供备用布局
还请注意,使用
sw600dp
和w600dp
之间存在差异——有关更多信息,请参见此问题:要提供替代布局资源,您需要在res文件夹中创建一个子目录,名为layout-sw600dp
,并将更改后的布局放在其中,与其他小屏幕布局的确切名称相同。然后,您的活动将自动查找此文件夹。因此,如果您的xml文件标题为activity\u main.xml
,那么它在layout-sw600dp
文件夹中也应该具有完全相同的名称
您还可以通过为大屏幕创建名为layout land
或layout-sw600dp-land
的子目录,为景观提供备用布局
还请注意,使用
sw600dp
和w600dp
之间存在差异——更多信息请参见此问题:Android Studio已自动为我完成此操作。我只是不明白为什么不加载。如果你想在用户旋转屏幕时更改布局,你需要使用layout land
。如果您只是想在更大的屏幕上使用不同的布局,请使用sw600dp
而不是w600dp
。关于这两个问题的更多信息,请参见我在回答中链接的问题。我希望它在几乎任何足够宽的屏幕上都能以同样的方式工作,景观智能手机就是一个例子,它也可以是平板电脑或其他任何东西。啊,我明白了。我想提到的另一件事是,我注意到您正在使用单独的活动
和内容
xml文件。确保为内容
xml文件创建备用文件。在您的图像中,您似乎只为活动
xml文件创建了单独的资源。Android Studio已经自动为我创建了这些资源。我只是不明白为什么不加载。如果你想在用户旋转屏幕时更改布局,你需要使用layout land
。如果您只是想在更大的屏幕上使用不同的布局,请使用sw600dp
而不是w600dp
。有关这两个问题的更多信息,请参见我在回答中链接的问题。我希望它在任何足够宽的屏幕上都能以同样的方式工作,而横向智能手机就是这样