2014-03-28 22 views
0

我意識到,在dimens.xml我必須指定要在整個應用程序使用的值的選項。所以,我可以建立類似:我怎麼能在所有的屏幕尺寸設置的Android字體尺寸只有一次?

<dimen name="layoutFont">22dp</dimen> 

然而,我有它設置的方式,然後我不得不爲layoutLargeFontlayoutXlargeFont建立類似的聲明,然後我必須手動更改佈局中的所有這些變量文件。

有沒有辦法做一個聲明一個變量:layoutFont其值,當它在一個或大或XLARGE佈局採用的是不同的?

+1

請使用'sp',而不是'dp',對於字體大小,讓您的字體大小可以與用戶的首選基地字體放大規模。 – CommonsWare

回答

2

可以指定你的主題TextAppearance。這將被應用到任何的TextView(和TextView中的子類)使用該主題的默認文本外觀。

<style name="MyApp.Theme" parent="android:Theme.Holo"> 
    <!-- other theme attrs --> 
    <item name="android:textAppearance">@style/TextAppearance</item> 
</style> 

<style name="TextAppearance" parent="android:TextAppearance"> 
    <item name="android:textSize">@dimen/layout_font</item> 
</style> 

你可以走了一步,並定義textAppearanceSmalltextAppearanceMedium,並textAppearanceLarge

<style name="MyApp.Theme" parent="android:Theme.Holo"> 
    <!-- other theme attrs --> 
    <item name="android:textAppearance">@style/TextAppearance</item> 
    <item name="android:textAppearanceSmall">@style/TextAppearance.Small</item> 
    <item name="android:textAppearanceMedium">@style/TextAppearance.Medium</item> 
    <item name="android:textAppearanceLarge">@style/TextAppearance.Large</item> 
</style> 

<style name="TextAppearance" parent="android:TextAppearance"> 
    <item name="android:textSize">@dimen/layout_font</item> 
</style> 

<style name="TextAppearance.Small"> 
    <item name="android:textSize">@dimen/layout_font_small</item> 
</style> 

<style name="TextAppearance.Medium"> 
    <item name="android:textSize">@dimen/layout_font_medium</item> 
</style> 

<style name="TextAppearance.Large"> 
    <item name="android:textSize">@dimen/layout_font_large</item> 
</style> 

當然,你將定義使用適當的資源預選賽這些尺寸(我建議最小寬度預選賽,-sw ### DP,爲described herealso here)。有了這一切,除非出於特定目的,否則不必爲任何東西設置任何textSize。

3

添加dimens.xml在

values-large and values-xlarge 
+0

http://developer.android.com/guide/practices/screens_support.html#qualifiers下面是相關文檔 – rperryng