如何在活动Android中处理2种不同的布局 [英] How to handle 2 different layouts in an activity Android

查看:100
本文介绍了如何在活动Android中处理2种不同的布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[已编辑]: 我已经开发了一个可以在不同屏幕尺寸的手机和平板电脑上正常运行的应用程序,按照Android指南,我按照其创建了不同的UI布局集,以适应不同尺寸的屏幕(手机和平板电脑),并且所有布局均为纵向模式.到那时一切正常,但是现在我有了一个新的要求,即平板电脑的布局将被更改,即它将具有比手机布局更多的信息,所有新的布局都将处于横向模式.

现在我的疑问是:

  1. 在旧的实现中,所有布局名称在不同的布局文件夹中均相同,即layout,layout-small,layout-large等...因此,根据新要求,所有大和超大布局均应处于横向模式但是,如果我使用相同的版式名称,那么如何处理方向?目前,清单文件中提到的每个活动的方向属性.
  2. 如果我保留这些布局的不同名称,则必须在运行时进行处理,即在加载活动时,基于检查电话屏幕尺寸,然后加载相应的布局,因此同一活动可以基于检查来加载不同的布局屏幕尺寸.这将是一种最佳方法吗?
  3. 或者,我是否必须为所有新布局完全引入新的活动以应对这种情况?

请向我建议处理这种情况的最佳方法.

解决方案

我建议您用相同的名称命名所有相似的文件.

充分利用Android提供的限定符,例如大小或设置的宽度限定符.对于小屏幕电话,您可能具有单窗格布局,而对于平板电脑,您可能具有多窗格布局.对于横向放置,提供多窗格布局(您可以将两个现有布局合并为一个)是一个很好的建议.

例如:

res/layout/onepane.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/frag1"
          android:layout_height="fill_parent"
          android:name="com.example.android.dummyApp"
          android:layout_width="match_parent" 
          android:background="@drawable/bg"/>
          </LinearLayout>

res/layout/twopanes.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/fragone"
          android:layout_height="fill_parent"
          android:name="com.example.android.dummyApp"
          android:layout_width="400dp"
          android:layout_marginRight="10dp"
          android:background="@drawable/bg"/>
<fragment android:id="@+id/fragtwo"
          android:layout_height="fill_parent"
          android:name="com.example.android.dummyApp"
          android:layout_width="fill_parent" />
         </LinearLayout>

现在已经定义了所有可能的布局,只需使用配置限定符将正确的布局映射到每个配置即可.您现在可以使用布局别名技术来做到这一点:

res/values/layouts.xml:

<resources>
<item name="main_layout" type="layout">@layout/onepane</item>
<bool name="has_two_panes">false</bool>
</resources>

res/values-sw600dp-land/layouts.xml:

<resources>
<item name="main_layout" type="layout">@layout/twopanes</item>
<bool name="has_two_panes">true</bool>
</resources>

注意:值文件夹中两个xml文件中的布尔值.

最小宽度限定符使您可以定位具有dp中给定的最小最小宽度的屏幕.请使用sw600dp而不是大型限定符,以表明两窗格布局适用于最小宽度为600 dp的屏幕.

另外,另一种解决方案:

如果您打算针对不同的屏幕尺寸进行不同的活动,则可以为同一项目构建多个apk.您要发布的每个APK应该有一个单独的Android项目.

请查看此链接以获取有关多个APK的更多信息. http://developer.android.com/training/multiple-apks/screensize.html

[Edited]: I have already developed one application which works well on different screen size phones and tablets, I followed as per android guidelines to create the different sets of UI layouts to cater with different sizes of screens(phones and tablets) and all layouts will be in portrait mode. Everything works fine till then but now I got one new requirement where the layouts of tablets will be changed i.e. it will be having some extra information than phone layouts and all new layouts will be in landscape mode.

Now my doubts are:

  1. In old implementation, all layouts name are same in different layout folders i.e. layout,layout-small,layout-large etc... so , as per new requirement, all layouts of large and extra-large should be in landscape mode but if i keep the same name of layout then how do I handle the orientation? At present, orientation attribute mentioned in manifest file for each activity.
  2. If I keep the different name of these layouts then I have to handle at run time i.e. while loading the activity,based on checking on phone screen size and then load the respective layout, so same activity can load different layout based on checking screen size. Will it be a optimal approach to do so?
  3. OR,Do I have to altogether introduce new activities for all new layouts to handle this scenario?

Please suggest me best approach to handle this scenario.

解决方案

I would suggest you to name all similar files with the same name.

Take full advantage of the qualifiers provided by Android like the size, or the set width qualifier. You may have a single pane layout for small screen phones and multi pane layout for tablets. It's a good advice to provide multi pane layout ( you may combine two existing layouts into one ) for landscape orientation.

For example :

res/layout/onepane.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/frag1"
          android:layout_height="fill_parent"
          android:name="com.example.android.dummyApp"
          android:layout_width="match_parent" 
          android:background="@drawable/bg"/>
          </LinearLayout>

res/layout/twopanes.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/fragone"
          android:layout_height="fill_parent"
          android:name="com.example.android.dummyApp"
          android:layout_width="400dp"
          android:layout_marginRight="10dp"
          android:background="@drawable/bg"/>
<fragment android:id="@+id/fragtwo"
          android:layout_height="fill_parent"
          android:name="com.example.android.dummyApp"
          android:layout_width="fill_parent" />
         </LinearLayout>

Now that all possible layouts are defined, it's just a matter of mapping the correct layout to each configuration using the configuration qualifiers. You can now do it using the layout alias technique:

res/values/layouts.xml:

<resources>
<item name="main_layout" type="layout">@layout/onepane</item>
<bool name="has_two_panes">false</bool>
</resources>

res/values-sw600dp-land/layouts.xml:

<resources>
<item name="main_layout" type="layout">@layout/twopanes</item>
<bool name="has_two_panes">true</bool>
</resources>

Note : The bool values in the two xml files in values folder.

The Smallest-width qualifier allows you to target screens that have a certain minimum width given in dp. Instead of the large size qualifier, use sw600dp to indicate the two-pane layout is for screens on which the smallest-width is 600 dp.

Also, another solution:

If you are planning to make different activities for different screen sizes, you can build multiple apk for the same project. There should be a separate Android project for each APK you’re going to release.

Please look at this link for more information on Multiple APKs. http://developer.android.com/training/multiple-apks/screensize.html

这篇关于如何在活动Android中处理2种不同的布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆