假设需要适应320x240,480x320分辨率。在res目录下新建文件夹values-320x240, values-480x320。然后在文件夹 values ,values-320x240 和 values-480x320 下新建xml文件dimens.xml,该xml文件内容如下:
1
2
3
4
|
<? xml version = "1.0" encoding = "utf-8" ?> < resources > < dimen name = "btnTextSize" >14dip</ dimen > </ resources > |
针对不同的分辨率,btnTextSize的值不同。在布局文件中,用下面的方式引用该值:
<
TextView
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
android:gravity
=
"center"
android:id
=
"@+id/lblSet"
style
=
"@style/btntext"
android:textSize
=
"@dimen/btnTextSize"
>
</
TextView
>
layout中设置图片自适应大小,并且设置最大宽高,当图片的宽高大于设置的最大值时,宽高值为设置的最大值。
- <ImageView android:id="@+id/image_view"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:adjustViewBounds="true"
- android:maxWidth="42dp"
- android:maxHeight="42dp"
- android:scaleType="fitCenter"
- android:layout_marginLeft="3dp"
- android:src="@drawable/icon"
- />
关键代码:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:maxWidth="42dp"
android:maxHeight="42dp"