我对px,dp和sp之间的区别有很好的了解。问题是,当我在屏幕上放置TextView(或ImageView)时,dpi无关紧要。在160 dpi的3英寸高屏幕上,80px的文本大小将为1/2英寸或高度的1/6。在具有160 dpi的7英寸高屏幕上,它仍然是1/2英寸,但是现在是高度的1/14。
问题是,使用dp(或sp),手机上看起来不错的东西在平板电脑上消失了。
有没有办法指定百分比的屏幕尺寸,例如百分比?
我不确定是否可以按百分比指定尺寸,但是为什么不在dimen.xml文件中为屏幕定义不同的尺寸。对于所有屏幕尺寸,都很难使用相同的dp和sp值。
您需要创建不同的dimens.xml不同的资源文件夹
res/values-ldpi/dimens.xml
res/values-mdpi/dimens.xml
res/values-hdpi/dimens.xml
接下来向这些文件添加一些值
<!-- in values-ldpi/dimens.xml -->
<dimen name="textSize">25dip</dimen>
<!-- in values-mdpi/dimens.xml -->
<dimen name="textSize">20dip</dimen>
最终引用布局文件中的尺寸:
<TextView
android:layout_toRightOf="@id/at"
android:layout_below="@id/hw"
android:textSize="@dimen/textSize"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
编辑:
如果要将图像高度设置为屏幕尺寸的1/10,则需要在视图容器中使用android:layout_weight属性。检查以下示例。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_weight="1"
>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/imageView"
android:background="@drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:weightSum="1"
android:layout_weight="9"
>
</LinearLayout>
</LinearLayout>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句