我研究过许多相关主题,但主题主要围绕图像展开。我明白这一点:http://developer.android.com/guide/practices/screens_support.html但我真的不明白动态调整字体大小的想法。
对于图像没有问题,我使用了 ldpi、mdpi、hdpi 和 xhdpi。但是对于字体,我不知道如何调整它们的大小。有人吗?
我的代码如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layoutHeader"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/headerimage"
android:orientation="horizontal">
<TextView
android:id="@+id/headerTitle"
android:layout_width ="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textColor="#FFF"
android:textSize="28dp"
android:textStyle="bold"
android:text="Some Random Banner Title"
style="@style/headerTextStyle"
/>
<ImageButton
android:id="@+id/btnPlus"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="5dp"
android:layout_marginTop="6dp"
android:background="#0000"
android:scaleType="centerCrop"
android:src="@drawable/plus" />
<ImageButton
android:id="@+id/btnInfo"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="6dp"
android:background="#0000"
android:scaleType="centerCrop"
android:src="@drawable/info" />
<ImageButton
android:id="@+id/btnGuide"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/btnPlus"
android:background="#0000"
android:scaleType="centerCrop"
android:src="@drawable/guide" />
</RelativeLayout>
最佳答案
我会把文本大小放在一个样式资源中,然后使用屏幕大小的替代样式资源文件..
因此您将拥有 res/values-large/style.xml、res/values-small/style.xml 等。这些样式中的每一个都将包含具有不同 dp 值的文本大小的项目。请记住,hdpi、mdpi、ldpi 等与像素密度有关,而不是屏幕尺寸。
例如,如果在 res/values/style.xml 中你有:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="headerTextStyle" >
<item name="android:textSize">28dp</item>
...
</style>
</resources>
在另一个资源文件中,假设大屏幕(res/values-large/style.xml):
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="headerTextStyle" >
<item name="android:textSize">46dp</item>
...
</style>
</resources>
然后当然将此样式应用于元素,系统将根据设备的屏幕尺寸选择正确的样式。您可以在同一个文件中提供多种样式,一种用于标题文本,一种用于常规文本 - 随便什么您需要的不同类型的文本,类似于 CSS。
或者,您可以将布局文件复制到类似限定的资源目录中,例如 res/layout-large/layout.xml、res/layout-small/layout.xml 并在每个目录中使用不同的文本大小,但这将是重复的不必要的更多 xml。
关于android - 支持多种屏幕尺寸的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13170179/