简体   繁体   English

您如何确定在多个布局之间切换的活动最后使用哪种布局?

[英]How do you determine which layout was last used by an activity that switches between multiple layouts?

I'm just starting to use fragments in my app and the idea is one column for screens of a certain width (or less) and two for wider screens. 我刚开始在我的应用程序中使用片段,其想法是一列用于一定宽度(或更小)的屏幕,而另一列用于宽屏幕。 So I have two separate layout files and in the onCreate method of MainActivity I choose which one to show based on the screen width. 因此,我有两个单独的布局文件,在MainActivity的onCreate方法中,我根据屏幕宽度选择显示哪个。 Since I am interested in the screen width rather than the orientation I can't use the simpler option of 'layout-land'. 由于我对屏幕的宽度而不是方向感兴趣,所以我不能使用“布局-土地”这个更简单的选项。 Both layouts use the same fragments but they can't be hard coded into the layout files because some of them need to be add added and removed at runtime - thus I use a fragment transaction in the onCreate method to (at the moment) just add the fragments. 两种布局都使用相同的片段,但是无法将它们硬编码到布局文件中,因为其中一些需要在运行时添加和删除-因此,我在onCreate方法中使用了片段事务(目前)只是添加碎片。

The problem comes when the activity is destroyed and recreated. 当活动被销毁并重新创建时,问题就来了。 If I don't check for whether savedinstancestate is null, it adds the fragments again (which is to be expected) and everything is doubled up. 如果我不检查saveinstancestate是否为null,它将再次添加片段(这是可以预期的),并且所有内容都会加倍。 But if I only do the create code when it's null - as you would if there was only one layout - then when I test screen width again and just use setContentView(one or the other layout) it recreates the one that was shown with no problem but the other is blank. 但是,如果我只在null时才执行创建代码(就像只有一种布局时那样),那么当我再次测试屏幕宽度并仅使用setContentView(一种或另一种布局)时,它会重新创建显示的页面而没有问题但另一个是空白。 Again that's to be expected because the second one hasn't been instantiated yet. 同样,这是可以预期的,因为第二个实例尚未被实例化。 So is it possible to determine from the savedinstancestate which layout was in use when the activity was destroyed? 因此,是否可以从saveinstancestate确定活动被销毁时正在使用的布局? And if it is, is it possible (or safe) to use the information in that to create the other layout - or should I just run the create code again? 如果可以的话,是否有可能(或安全地)使用其中的信息来创建其他布局-还是应该再次运行创建代码? In other words does the standard savedinstancestate persist all the data I need when more than one layout is in use or will I have to do it all myself? 换句话说,当使用多个布局时,标准的saveinstancestate是否会保留我需要的所有数据,还是我必须自己完成所有工作?

You can still use resource buckets to contain your layouts. 您仍然可以使用资源桶来包含布局。 ie: 即:

use / layout-sw600dp / that is layout smallest width of 600 dip 使用/ layout-sw600dp /即布局最小宽度为600dip

From the official documentation http://developer.android.com/training/multiscreen/screensizes.html#TaskUseSWQuali 从官方文档http://developer.android.com/training/multiscreen/screensizes.html#TaskUseSWQuali

res/layout/main.xml, single-pane (default) layout: res / layout / main.xml,单窗格(默认)布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fragment android:id="@+id/headlines"
              android:layout_height="fill_parent"
              android:name="com.example.android.newsreader.HeadlinesFragment"
              android:layout_width="match_parent" />
</LinearLayout>

res/layout-sw600dp/main.xml, two-pane layout: res / layout-sw600dp / main.xml,两窗格布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal">
    <fragment android:id="@+id/headlines"
              android:layout_height="fill_parent"
              android:name="com.example.android.newsreader.HeadlinesFragment"
              android:layout_width="400dp"
              android:layout_marginRight="10dp"/>
    <fragment android:id="@+id/article"
              android:layout_height="fill_parent"
              android:name="com.example.android.newsreader.ArticleFragment"
              android:layout_width="fill_parent" />
</LinearLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过两个布局检测活动中正在使用的布局 - Detect which layout is in use in an activity with two layouts 如何在已经存在的两个布局之间显示或插入新的布局? - How to display or insert new layout between two layouts which are already there? 隐藏和显示android布局-如何标记活动布局? - Hiding and showing android layouts - How do you mark active layout? 如何使活动在基于Intent切换到新布局之前等待? - How do I make the Activity wait before it switches to the new layout based on an Intent? 如何向活动添加多个布局 - How to add multiple layouts to the activity 如何将画布绘图与包含按钮和布局的android活动布局相结合? - How to combine canvas drawing with android activity layout which includes buttons and layouts? 确定Android相机活动所使用的相机 - Determine which camera used by Android camera activity 当用户在 Android 中在明暗模式之间切换时,如何控制使用什么 colors - How do I control what colors are used when a user switches between Light and Dark mode in Android 如何使用尽可能小的布局文件夹组织活动布局? - How do I organize my activity layouts with the smallest possible layout folders? 片段活动布局如何受到主要活动布局的影响? - How are fragment activity layouts affected by main activity layout?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM