假設需要適應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
>
|
1
2
3
4
|
<
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"