假设需要适应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"