Android活动加载错误的布局文件

时间:2016-03-28 21:33:17

标签: android android-layout android-fragments

我正在开发一个简单的应用程序,您可以在其中查看音乐家列表,当您点击音乐家时,您可以获得有关它的一些详细信息(流派,热门歌曲和传记)。 我想要实现的功能是:如果你在宽屏幕上观看它,请让音乐家列表在左边,细节应该出现在右边(一旦点击一个音乐家);如果它是一个窄屏幕,细节应该在新屏幕上单独出现。功能应该使用片段和Framelayouts来完成。

所以我有activity_main.xml,我去了添加新资源文件 - >布局文件,我添加了activity_main.xml (w600dp),我希望自动加载在面向横向的智能手机或平板电脑上。

activity_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>

w600dp / activity_main.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">
    <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);
        }
    }

FragmentDetaljiFragmentLista除了使用onCreateView之外没有太多特别之处(尽管我可以添加它们,如果它们可能是麻烦的来源)。

问题在于,无论我用于加载此应用程序的是什么,activity_main都以其默认形式加载,而不是w600dp版本!我做错了什么?

1 个答案:

答案 0 :(得分:0)

要提供备用布局资源,您需要在名为layout-sw600dp的res文件夹中创建一个子目录,并将更改后的布局放在那里,使用EXACT名称作为另一个小屏幕布局。然后您的Activity应自动查找此文件夹。因此,如果您的xml文件标题为activity_main.xml,那么它在layout-sw600dp文件夹中也应该具有完全相同的名称。

您还可以通过为大屏幕创建名为layout-landlayout-sw600dp-land的子目录来为横向提供备用布局。

另请注意,使用sw600dpw600dp之间存在差异 - 有关详细信息,请参阅此问题:Difference between sw600dp and w600dp?