Android 为安卓手机和平板电脑创建不同的布局
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24074226/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating different layout for android phone and tablet
提问by user3713706
This is a basic android question. I have app which need to have different screen design for a phone and a tablet. The phone needs to have a ListView and the Tablet need to have a GridView of items.
这是一个基本的android问题。我有需要为手机和平板电脑设计不同屏幕的应用程序。手机需要有一个 ListView,平板电脑需要有一个项目的 GridView。
I wish to know how can I do this without making two different apps.
我想知道如何在不制作两个不同的应用程序的情况下做到这一点。
Thanks
谢谢
采纳答案by vyas
Basically you have to make different layouts for both android phone and tablets. Android is smart enough to differentiate. For example for large screen you can just make a new folder namer Layout-large. and put your tablet xml in it. Android will pick xml from here and in case of phone it will pick from simple layout folder. The configuration qualifiers you can use to provide size-specific resources are small, normal, large, and xlarge. For example, layouts for an extra large screen should go in layout-xlarge/.
基本上你必须为安卓手机和平板电脑制作不同的布局。Android 足够聪明,可以区分。例如,对于大屏幕,您可以创建一个新文件夹命名为 Layout-large。并将您的平板电脑 xml 放入其中。Android 将从这里选择 xml,如果是手机,它将从简单的布局文件夹中选择。可用于提供特定于大小的资源的配置限定符是 small、normal、large 和 xlarge。例如,超大屏幕的布局应该放在 layout-xlarge/ 中。
I would recommend if both phone and tablet screens and totally different you can make two different apks and load on same Id on google play. This way your application will be light weight and will be be fast. Google play automatically can detect that application is for tablet or or phone.You need not to worry about that.
我建议如果手机和平板电脑屏幕完全不同,您可以制作两个不同的 apk 并在 google play 上加载相同的 Id。这样你的应用程序将是轻量级的,并且速度很快。Google Play 会自动检测该应用程序是用于平板电脑还是手机。您无需担心。
回答by matiash
You basically need to provide different layout files for different qualifiers.
您基本上需要为不同的限定符提供不同的布局文件。
As a very brief example, xml layout files places in the layout-sw720dp
(or layout-large
for API level < 13) folder will be used for devices more than 720 dips wide (i.e. 10" tablets).
作为一个非常简短的示例,位于layout-sw720dp
(或layout-large
API 级别 < 13)文件夹中的 xml 布局文件将用于宽度超过 720 倾角的设备(即 10 英寸平板电脑)。
Check Supporting Multiple Screensand Providing Resourcesin the Android documentation.
在 Android 文档中查看Supporting Multiple Screens和提供资源。
回答by Shivam Verma
Check this out : http://developer.android.com/guide/practices/screens_support.html
看看这个:http: //developer.android.com/guide/practices/screens_support.html
You do not need to make two separate apps.
您不需要制作两个单独的应用程序。
回答by SandaruMK
Here is what I did for the same changing the Recycler View's "app:layoutManager" attribute. Since you are using a list and grid view using the recycler view seems to be the best way to have your scrolling smoothly.
这是我对 Recycler View 的“app:layoutManager”属性所做的相同更改。由于您使用回收器视图使用列表和网格视图似乎是让您顺利滚动的最佳方式。
- Create a folder in res "layout-sw600dp".
- Copy the layout file to this folder.
In the original layout file which is in the layout folder, set linear layout manager as the layout manager attribute.
<android.support.v7.widget.RecyclerView app:layoutManager="android.support.v7.widget.LinearLayoutManager"/>
In the "layout-sw600dp" folder layout file, set GridLayoutManager as the Recycler View attribute.
<android.support.v7.widget.RecyclerView app:layoutManager="android.support.v7.widget.GridLayoutManager"/>
- 在 res“layout-sw600dp”中创建一个文件夹。
- 将布局文件复制到此文件夹。
在布局文件夹中的原始布局文件中,将线性布局管理器设置为布局管理器属性。
<android.support.v7.widget.RecyclerView app:layoutManager="android.support.v7.widget.LinearLayoutManager"/>
在“layout-sw600dp”文件夹布局文件中,设置GridLayoutManager为Recycler View属性。
<android.support.v7.widget.RecyclerView app:layoutManager="android.support.v7.widget.GridLayoutManager"/>
Do not forget to change the span count because it defaults to a 0 which will be same as the output of the liner layout.
不要忘记更改跨度计数,因为它默认为 0,这将与线性布局的输出相同。
Resources:
资源: