【Android开发经验】兼容不同的屏幕大小(推荐,最官方的适应屏幕

转载请注明出处:

由于Android设备的碎片特性,关于屏幕适配的话题一直绵绵不休,这篇文章是Android开发者官网的屏幕适配教程,算是非常官方的解决方案,我们可以从这里学到很多。

原文链接:

转自:

这节课教你如何通过以下几种方式支持多屏幕: 确保你的布局能自适应屏幕 根据你的屏幕配置提供合适的UI布局 确保你当前的布局适合当前的屏幕 提供合适的位图(bitmap)

1.使用“wrap_content”和“match_parent”

为了确保你的布局能灵活的适应不同的屏幕尺寸,针对一些view组件,你应该使用wrap_content和match_parent来设置他们的宽和高。如果你使用了wrap_content,view的宽和高会被设置为该view所包含的内容的大小值。如果是match_parent(在API 8之前是fill_parent)则被设置为该组件的父控件的大小。 通过使用wrap_content和match_parent尺寸值代替硬编码的尺寸,你的视图将分别只使用控件所需要的空间或者被拓展以填充所有有效的空间。比如:

<LinearLayout xmlns:android=""android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><LinearLayout android:layout_width="match_parent"android:id="@+id/linearLayout1"android:gravity="center"android:layout_height="50dp"><ImageView android:id="@+id/imageView1"android:layout_height="wrap_content"android:layout_width="wrap_content"android:src="@drawable/logo"android:paddingRight="30dp"android:layout_gravity="left"android:layout_weight="0" /><View android:layout_height="wrap_content"android:id="@+id/view1"android:layout_width="wrap_content"android:layout_weight="1" /><Button android:id="@+id/categorybutton"android:background="@drawable/button_bg"android:layout_height="match_parent"android:layout_weight="0"android:layout_width="120dp"style="@style/CategoryButtonStyle"/></LinearLayout><fragment android:id="@+id/headlines"android:layout_height="fill_parent"android:name="com.example.android.newsreader.HeadlinesFragment"android:layout_width="match_parent" /></LinearLayout> 注意上面的例子使用wrap_content和match_parent来指定组件尺寸而不是使用固定的尺寸。这样就能使你的布局正确的适配不同的屏幕尺寸和屏幕配置(这里的配置主要是指屏幕的横竖屏切换)。 例如,下图演示的就是该布局在竖屏和横屏模式下的效果,注意组件的尺寸是自动适应宽和高的。

2.使用相对布局(RelativeLayout)

你可以使用LinearLayout以及wrap_content和match_parent组合来构建复杂的布局,但是LinearLayout却不允许你精准的控制它子view的关系,子view在LinearLayout中只能简单一个接一个的排成行。如果你需要你的子view不只是简简单单的排成行的排列,更好的方法是使用RelativeLayout,它允许你指定你布局中控件与控件之间的关系,比如,你可以指定一个子view在左边,另一个则在屏幕的右边。

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android=""android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:id="@+id/label"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Type here:"/><EditTextandroid:id="@+id/entry"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_below="@id/label"/><Buttonandroid:id="@+id/ok"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/entry"android:layout_alignParentRight="true"android:layout_marginLeft="10dp"android:text="OK" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_toLeftOf="@id/ok"android:layout_alignTop="@id/ok"android:text="Cancel" /></RelativeLayout> 3.使用据尺寸限定词

这里的限定词只要是指在编写布局文件时,将布局文件放在加上类似large,sw600dp等这样限定词的文件夹中,以此来告诉系统根据屏幕选择对应的布局文件,比如下面例子的layout-large文件夹 我们知道如何编写灵活的布局或者相对布局,它们都能通过拉伸或者填充控件来适应不同的屏幕,但是它们却无法为每个不同屏幕尺寸提供最好的用户体验。因此,你的应用不应该只是实现灵活的布局,同时也应该为不同的屏幕配置提供几种不同的布局方式。你可以通过配置限定(configuration qualifiers)来做这件事情,它能在运行时根据你当前设备的配置(比如不同的屏幕尺寸设计了不同的布局)来选择合适的资源。 比如,很多应用都为大屏幕实现了“两个方框”模式(应用可能在一个方框中实现一个list,另外一个则实现list的content),,平板和电视都是大到能在一个屏幕上适应两个方框,但是手机屏幕却只能单个显示。所以,如果你想实现这些布局,你就需要以下文件: res/layout/main.xml.单个方框(默认)布局:

<LinearLayout xmlns: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,两个方框布局:<LinearLayout xmlns: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> 注意第二个布局文件的目录名字“large qualifier”,在大尺寸的设备屏幕时(比如7寸平板或者其他大屏幕的设备)就会选择该布局文件,而其他比较小的设备则会选择没有限定词的另一个布局(也就是第一个布局文件)。

4.使用最小宽度限定词走过的路成为背后的风景,不能回头不能停留,若此刻停留,

【Android开发经验】兼容不同的屏幕大小(推荐,最官方的适应屏幕

相关文章:

你感兴趣的文章:

标签云: