2012-11-01 79 views
1

我已經研究了許多相關的主題,但主題主要圍繞圖像。我明白了這一點: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> 

回答

4

我會把文字大小在樣式資源,然後用另一種樣式的資源文件的屏幕尺寸..

這樣你纔會有RES /值-large/style.xml,res/values-small/style.xml等。這些樣式中的每一個都將包含具有不同dp值的文本大小的項目。請記住,hdpi,mdpi,ldpi等與像素密度有關,而不是屏幕尺寸。

因此,舉例來說,如果在res /價值/ style.xml您有:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
<style name="headerTextStyle" > 
    <item name="android:textSize">28dp</item> 
    ... 
</style> 
</resources> 
在另一個資源文件

,假設爲大屏幕(RES /價值觀大/ 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。