创建多屏支持应用程序android

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22728159/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:23:27  来源:igfitidea点击:

Creating multi-screen support app android

androidlayoutscreenandroid-screen-support

提问by Jay Vyas

I am creating a app in Android that must support multiple screens.

我正在 Android 中创建一个必须支持多个屏幕的应用程序。

For this purpose i have read many answers and one which i like most is state that i can create different folders in resfolder by following name,

为此,我阅读了许多答案,我最喜欢的一个是声明我可以通过以下名称在res文件夹中创建不同的文件夹,

For small screens layout-small,

对于小屏幕layout-small

For large screens layout-large,

对于大屏幕layout-large

For extra large screens layout-xlarge

对于超大屏幕layout-xlarge

and i have to create different layouts for different screens with samename. e.g mylayout.xml

我必须为具有相同名称的不同屏幕创建不同的布局。例如 mylayout.xml

and i have to put following code in manifest.xml file

我必须将以下代码放在 manifest.xml 文件中

<supports-screens android:smallScreens="true" 
      android:normalScreens="true" 
      android:largeScreens="true"
      android:xlargeScreens="true"
      android:anyDensity="true" />

and finally i just need to write following lines in my Activity file,

最后我只需要在我的活动文件中写下以下几行,

  setContentView(R.layout.mylayout);

Now when i run this app in different size screens it should acquire layouts from different folders as per screen resolution.

现在,当我在不同尺寸的屏幕上运行这个应用程序时,它应该根据屏幕分辨率从不同的文件夹中获取布局。

But this is not happening in my case.It takes only layout that defined in layoutfolder.

但这在我的情况下不会发生。它只需要在布局文件夹中定义的布局

Please can some one explains me why this happening and how can i solve this issue so that my app can run effectively on all size screens.

请有人向我解释为什么会发生这种情况以及我如何解决这个问题,以便我的应用程序可以在所有尺寸的屏幕上有效运行。

回答by Arun Antoney

Try out like:

尝试如下:

  • layout-sw320dp
  • layout-sw480dp
  • layout-sw600dp
  • layout-sw720dp
  • 布局-sw320dp
  • 布局-sw480dp
  • 布局-sw600dp
  • 布局-sw720dp

instead of

代替

  • layout-small,
  • layout-large etc...
  • 布局小,
  • 布局大等...

回答by Sethu

Please refer below link:

请参考以下链接:

http://developer.android.com/guide/practices/screens_support.htmlFor Different screen size, The following is a list of resource directories in an application that provides different layout designs for different screen sizes and different bitmap drawables for small, medium, high, and extra high density screens. you could use different size of the layout files in res folder and also vary for drawable images based on the density..

http://developer.android.com/guide/practices/screens_support.html对于不同的屏幕尺寸,以下是一个应用程序中的资源目录列表,为不同的屏幕尺寸提供不同的布局设计,为小、中型提供不同的位图可绘制、高密度和超高密度屏幕。您可以在 res 文件夹中使用不同大小的布局文件,也可以根据密度对可绘制图像进行更改。

  res/layout/my_layout.xml             // layout for normal screen size ("default")
  res/layout-small/my_layout.xml       // layout for small screen size
  res/layout-large/my_layout.xml       // layout for large screen size
  res/layout-xlarge/my_layout.xml      // layout for extra large screen size
  res/layout-xlarge-land/my_layout.xml // layout for extra large in landscape orientation

enter image description hereres/drawable-mdpi/my_icon.png // bitmap for medium density res/drawable-hdpi/my_icon.png // bitmap for high density res/drawable-xhdpi/my_icon.png // bitmap for extra high density

在此处输入图片说明res/drawable-mdpi/my_icon.png // 中等密度的位图 res/drawable-hdpi/my_icon.png // 高密度的位图 res/drawable-xhdpi/my_icon.png // 超高密度的位图

<compatible-screens>
    <screen
        android:screenDensity="ldpi"
        android:screenSize="small" />
    <screen
        android:screenDensity="mdpi"
        android:screenSize="normal" />
    <screen
        android:screenDensity="xhdpi"
        android:screenSize="large" />
    <screen
        android:screenDensity="xhdpi"
        android:screenSize="xlarge" />        
</compatible-screens>

And followed by any activity use this lines..

然后任何活动都使用此行..

android:configChanges="orientation|screenSize|keyboardHidden"

android:configChanges="orientation|screenSize|keyboardHidden"

回答by Buru

In values folder naming convention like layout-smallonly works for devices with api version less than 3.1. You should create values file with naming like layout-sw600dpfor api level greater than 3.1. read this http://developer.android.com/guide/practices/screens_support.html3.1api

在 values 文件夹命名约定中,layout-small仅适用于带有api version less than 3.1. 您应该创建命名layout-sw600dp为 api 级别大于 3.1 的值文件。阅读这个http://developer.android.com/guide/practices/screens_support.html3.1api

enter image description herelike this you should create layout-sw600dp, layout-sw720dp for each type of devices. layout-sw600dp means this layout works for devices with smallest width of 600dp If you have layout-600dp and layout-sw720dp folders. first layout folder works for devices with smallest width of 600dp(7 inch tablet) to 720dp and second works for devices with smallest width above 720dp(10 inch tablet).

在此处输入图片说明像这样,您应该为每种类型的设备创建 layout-sw600dp、layout-sw720dp。layout-sw600dp 表示此布局适用于最小宽度为 600dp 的设备,如果您有 layout-600dp 和 layout-sw720dp 文件夹。第一个布局文件夹适用于最小宽度为 600dp(7 英寸平板电脑)到 720dp 的设备,第二个适用于最小宽度超过 720dp(10 英寸平板电脑)的设备。

If your minimum required version is above 3.1 you don't need have layout-small, layoutxLarge folders. otherwise you have to consider both type of layout fromats.

如果您所需的最低版本高于 3.1,则不需要 layout-small、layoutxLarge 文件夹。否则你必须考虑两种类型的布局 fromats。

回答by Sanjeevcn

Here is a quick checklistabout how you can ensure that your application displays properly on different screens:

以下是关于如何确保您的应用程序在不同屏幕上正确显示的快速清单

Use wrap_content, fill_parent,or dpunits when specifying dimensions in an XML layout file. Do not use hard coded pixelvalues in your application code(.javafiles). Do not use AbsoluteLayout(it's deprecated in Android 1.5). You should instead use RelativeLayout, which uses relative positioning to lay out its child views. Supply alternative bitmap drawables for different screen densities.

在 XML 布局文件中指定尺寸时使用wrap_content, fill_parent,dp单位。不要pixel在应用程序代码(.java文件)中使用硬编码值。不要使用AbsoluteLayout(在 中已弃用Android 1.5)。您应该改为使用RelativeLayout,它使用相对定位来布置其子视图。为不同的屏幕密度提供替代位图可绘制对象。

Take time and read these screens_supportor to get a better idea see How Android Finds the Best-matching Resourceso that you know where to place your resources.

花点时间阅读这些screen_support或获得更好的想法,请参阅Android 如何找到最匹配的资源,以便您知道将资源放在哪里。