Support text in multiple screen size

2019-08-08 09:16发布

问题:

I have researched on many relevant topics but the topics mainly revolve around images. I had understand this: http://developer.android.com/guide/practices/screens_support.html but I don't really get the idea of dynamic resizing for the fonts.

For images there's no problem, I used the ldpi, mdpi, hdpi and xhdpi. However for the fonts, I do not know how to resize them. Anyone?

My codes is as followed:

<?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>

回答1:

I would put the text sizes in a style resource, then use alternate style resource files for screen sizes..

so you will have res/values-large/style.xml, res/values-small/style.xml, etc. Each of those styles will contain the item for the text size with different dp values. Remember, hdpi, mdpi, ldpi etc have to do with pixel density, not screen size.

So for instance, if in res/values/style.xml you have:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="headerTextStyle" >
    <item name="android:textSize">28dp</item>
   ...
</style>
</resources>

in another resource file, suppose for large screen (res/values-large/style.xml) :

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="headerTextStyle" >
    <item name="android:textSize">46dp</item>
   ...
</style>
</resources>

Then of course apply this style to the element, and the system will pick the correct one based on the device's screen size.. You can provide multiple styles in the same file, one for header text, one for regular text - whatever distinct types of text you need, similar to CSS.

Alternatively, you could duplicate your layout file in similarly qualified resource directories, like res/layout-large/layout.xml, res/layout-small/layout.xml and have different text sizes in each, but this will be duplicating more xml unnecessarily.