Android支持不同屏幕尺寸的手机

  今天介绍一下关于Android支持不同屏幕尺寸的手机有些什么方法。首先支持不同屏幕尺寸的手机的设计准则:

1、确保你的布局可以适当调整大小以适应屏幕。

2、根据屏幕配置,提供适当的UI布局。

3、确保正确的布局应用到正确的屏幕。

4、正确提供适当缩放大小的位图。

  具体的方法有:

1、使用“ wrap_content ”和“ match_parent ”:为了确保你的布局非常灵活,适应不同的屏幕尺寸,你应该使用“ wrap_content ”和“ match_parent ”设置视图组件的宽度和高度。如果使用“ wrap_content ” ,视图的宽度或高度设置为所需的最小尺寸以适应内容在该视图,而“ match_parent ” (又称“ FILL_PARENT ” API级别8以下 )使得组件扩展到其父视图的大小相匹配。

2、使用RelativeLayout:你可以使用嵌套LinearLayout组合“ wrap_content ”和“ match_parent ”构建相当复杂的布局。然而, LinearLayout中不允许你能够精确地控制子视图的空间关系;如果您需要子视图为导向的变化不是一条直线,一个更好的解决办法是经常使用相对布局,它允许你指定你的布局组件之间的空间关系。例如,可以在左侧对齐一个子视图,也可以在屏幕的右侧对齐。

3、使用Size Qualifiers(大小标记符):虽然我们可以用上面的方法定义布局文件,让其在不同的情况下进行拉伸等动作,可是在某些比较大的屏幕,比如平板与TV上面还是不太适合,我们最好可以在这种情况下使用两套不同的布局文件来适配大小,我们可以使用大小标示符来标记不同的布局,让机器在运行程序的时候根据自身的大小来选择显示哪个布局。例如:res/layout/main.xml, single-pane (default) layout:

<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-large/main.xml, two-pane layout:

<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>

4、使用the Smallest-width Qualifiers(最小宽度标记符):在很多时候我们不方便判断具体多大的屏幕才叫做large[比如5“与7”的设备],所以在Android 3.2之后引入了sw600dp,这样的方式来表示那些屏幕宽度至少是600dp以上的设备[通常7“的设备都至少会有600dp的宽],我们这可以这样定义一个布局文件:res/layout-sw600dp/main.xml(这是一个two-pane的布局),这样设置之后,系统会在设备屏幕宽至少600dp的时候自动选择显示这个布局文件,当然小于的情况下就会选择默认的res/layout/main.xml布局文件。

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:

<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>


5、使用Layout Aliases(布局别名):因为3.2之前不支持使用Smallest-width的标识符,为了兼容之前的设备,我们仍然需要使用一些抽象的大小标识符(small, normal, large and xlarge),有时候我们会出现定义了重复的布局文件的情况,比如

res/layout/main.xml: single-pane layout
res/layout-xlarge: multi-pane layout (这个文件是用来兼容3.2之前的设备)
res/layout-sw600dp: multi-pane layout (这个文件可以在3.2上进行使用,但是与上面的布局文件一致,都是two-pane的布局)

为了避免这样的duplication(重复的)文件,我们可以为重复的布局取个别名:

res/layout/main.xml, single-pane layout
res/layout/main_twopanes.xml, two-pane layout

这样以后,我们可以这样重新定义上面那两个布局文件:

res/values-xlarge/layout.xml:

<resources>
    <item name="main" type="layout">@layout/main_twopanes</item>
</resources>

res/values-sw600dp/layout.xml:

<resources>
    <item name="main" type="layout">@layout/main_twopanes</item>
</resources>

这样一来,就只需写一份two-pane的布局文件,两个布局设置都引用它 [这个设计很容易理解,通常很多地方我们都有这样做过]。

6、使用Orientation Qualifiers [方向标识符]:一些布局可以很好的自动适配横屏landscape与竖屏portrait,但是我们最好是针对不同的方向设置不同的布局会比较好
比如我们现在有这样一个需求:
•small screen, portrait: single pane, with logo
•small screen, landscape: single pane, with logo
•7" tablet, portrait: single pane, with action bar
•7" tablet, landscape: dual pane, wide, with action bar
•10" tablet, portrait: dual pane, narrow, with action bar
•10" tablet, landscape: dual pane, wide, with action bar
我们可以使用上面说的别名方法,对上面的需求抽取出一些公共的布局元素,定义下面几个布局:
res/layout/onepane.xml;
res/layout/onepane_with_bar.xml;
res/layout/twopanes.xml;
res/layout/twopanes_narrow.xml;
这样以来,我们需要在setContentView的时候选择main_layout就可以了,系统会选择到相应的Value里面取出对应的布局文件进行显示:

res/values/layouts.xml:

<resources>
    <item name="main_layout" type="layout">@layout/onepane_with_bar</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>

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

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

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

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

res/values-large-port/layouts.xml:

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

7、使用Nine-patch Bitmaps(九宫图):一种有限制的可伸缩的图片格式,为了避免通常的图片在伸缩后出现的不某些不适配,比如图片中有个logo,你不希望这个logo随拉伸而拉伸,那么我们就应该使用这种格式的图片来定义哪些区域可以拉伸。我们可以使用draw9patch这个工具(位于tools/目录下)来把普通图片转换为9-patch格式的图片。

普通图片:

九宫格图片:

拉伸后的图片:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值