如何在android中支持不同的屏幕尺寸

Jac*_*rky 47 android screen

我正在开发一个Android应用程序,我必须支持所有不同的屏幕尺寸和密度.所以我为布局创建了不同的文件夹:layout-small layout-largelayout.

然后我为图像创建了不同的文件夹:ldpi, mdpihdpi.在所有可绘制文件夹中,图像必须具有不同的大小?我问这个原因我有一个屏幕尺寸大且密度适中的手机,显示的图像会更小而且尺寸不合适?

Utt*_*tam 77

对于不同的屏幕尺寸,以下是应用程序中的资源目录列表,它为不同的屏幕尺寸和不同的位图可绘制提供不同的布局设计,适用于小型,中型,高密度和超高密度屏幕.

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

res/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
Run Code Online (Sandbox Code Playgroud)

Manifest中的以下代码支持所有dpis.

<supports-screens android:smallScreens="true" 
          android:normalScreens="true" 
          android:largeScreens="true"
          android:xlargeScreens="true"
          android:anyDensity="true" />
Run Code Online (Sandbox Code Playgroud)

并查看我的答案.

  • 从布局中使用不同的文件夹,例如layout-small,从3.2开始不推荐使用 (15认同)

Sch*_*esi 11

从Android 3.2(API级别13)开始,不推荐使用大小组(文件夹small,normal,large,xlarge),以支持基于可用屏幕宽度管理屏幕大小的新技术.



您可以根据布局的可用空间指定不同的资源配置:

1)最小宽度 - 屏幕的基本尺寸,由可用屏幕区域的最短尺寸指示.

限定符值:sw'dp value'dp

例如.res/sw600dp/layout.xml - >将用于所有大于或等于600dp的屏幕尺寸.这不会考虑设备方向.


2)可用屏幕宽度 - 指定应使用资源的dp单位的最小可用宽度.

限定符值:w'dp value'dp

例如.res/w600dp/layout.xml - >将用于所有屏幕,宽度大于或等于600dp.


3)可用屏幕高度 - 指定应使用资源的dp单位的最小屏幕高度.

限定符值:h'dp value'dp

例如.res/h600dp/layout.xml - >将用于所有屏幕,其高度大于或等于600dp.



所以最后你的文件夹结构可能如下所示:

res/layout/layout.xml - >用于手机(小于600dp可用宽度)
res/layout-sw600dp/layout.xml - >用于7"平板电脑(600dp宽大) )
res/layout-sw720dp/layout.xml - > 10"平板电脑(720dp宽大)


有关详细信息,请阅读官方文档:https:
//developer.android.com/guide/practices/screens_support.html#DeclaringTabletLayouts


Elh*_*aky 10

您可以使用sdp size unit而不是dp size unit.所述SDP大小单位是相对于屏幕尺寸,并且因而经常优选用于靶向多种屏幕尺寸.

小心使用!例如,在大多数情况下,您仍需要为平板电脑设计不同的布局.


Tri*_*oya 5

当涉及到支持多种屏幕尺寸时,听起来不错。以下提供了更好的结果。

res/layout/layout-w120dp
res/layout/layout-w160dp
res/layout/layout-w240dp
res/layout/layout-w160dp
res/layout/layout-w320dp
res/layout/layout-w480dp
res/layout/layout-w600dp
res/layout/layout-w720dp
Run Code Online (Sandbox Code Playgroud)

使用显示指标检查设备的宽度和高度

放置/显示哪种布局适合设备的最终宽度。

let smallestScreenWidthDp="assume some value(Which will be derived from Display metrics)"
Run Code Online (Sandbox Code Playgroud)

应该在setContentView()之前检查所有内容,否则会遇到麻烦

     Configuration config = getResources().getConfiguration();

     Configuration config = getResources().getConfiguration();

        if (config.smallestScreenWidthDp >= 600) {
            setContentView(R.layout.layout-w600dp);
        } else {
            setContentView(R.layout.main_activity);
        }
Run Code Online (Sandbox Code Playgroud)

在顶部,我已经创建了许多布局以适合多个屏幕,这取决于您,可能与不取决于您。您可以从Play商店查看哪个API的评论,下载量很高。 。

我希望它对您有帮助。很少使用一些第三方库,这可能会减少您的工作量,但这不是最佳实践。习惯于Android最佳做法。

看一下这个


Dee*_*hia 5

使用Github 提供的sdp 库