首页  > 02年世界杯巴西

Android自适应不同屏幕大小的全部方法

本文讲述了Android自适应不同屏幕大小的全部方法。分享给大家供大家参考,具体如下:

本文将告诉你如何让你的应用程序支持各种不同屏幕大小,主要通过以下几种办法:

让你的布局能充分的自适应屏幕;

根据屏幕的配置来加载合适的UI布局;

确保正确的布局应用在正确的设备屏幕上;

提供可以根据屏幕大小自动伸缩的图片;

使用 "wrap_content" 和 "match_parent"。

为了确保你的布局能够自适应各种不同屏幕大小,你应该在布局的视图中使用"wrap_content"和"match_parent"来确定它的宽和高。如果你使用了"wrap_content",相应视图的宽和高就会被设定成刚好能够包含视图中内容的最小值。而如果你使用了"match_parent"(在Android API 8之前叫作"fill_parent"),就会让视图的宽和高延伸至充满整个父布局。

通过使用"wrap_content"和"match_parent"来替代硬编码的方式定义视图大小,你的视图要么仅仅使用了需要的那边一点空间,要么就会充满所有可用的空间。例如:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:id="@+id/linearLayout1"

android:gravity="center"

android:layout_height="50dp">

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

android:id="@+id/view1"

android:layout_width="wrap_content"

android:layout_weight="1" />