2011-12-05 59 views
10

我正在嘗試在全局級別設置文本大小,並且無法找到或找到執行此操作的方法。Android:我如何設置佈局的文本大小?

例如,我有這樣的事情:

<?xml version="1.0" encoding="utf-8"?> 

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id = "@+id/Table" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:shrinkColumns="*" 
     android:stretchColumns="*"> 
</TableLayout> 
</ScrollView> 

的TableRows和TextViews本身產生動態地取決於用戶輸入。

問題是我想基於資源文件全局設置基本textSize,而不必去和觸摸一堆代碼。有沒有辦法告訴應用程序,「嘿應用程序,用它作爲所有textViews的基本文本大小?」

或者換句話說,在HTML中,我可以在body,div和table級別設置字體大小。在佈局(LinearLayout,TableLayout,ScrollView等)級別上可以做類似的事情嗎?

回答

13

要設置所有TextView的全局樣式,請在自定義主題中設置android:textViewStyle屬性並將該主題應用於您的應用程序或單個活動。

僞代碼:

<style name="MyTheme" parent="android:Theme"> 
    <item name="android:textViewStyle">@style/MyTextView</item> 
</style> 

<style name="MyTextView" parent="android:Widget.TextView"> 
    <item name="android:textSize">14sp</item> 
</style> 

... 

<activity android:theme="@style/MyTheme"> 
    ... 
+0

感謝該訣竅。我搜索了幾個小時並找不到它。再次感謝。 –

+0

甜蜜,樂意幫忙! –

+1

另外值得一提的是,「你應該更喜歡使用sp(scale-independent pixel)來定義文本大小。sp比例因子取決於用戶設置,系統按照dp的尺寸調整大小。」 http://developer.android.com/guide/practices/screens_support.html#screen-independence – scottyab

0

或者,如果你想設置默認各地的文字風格,但覆蓋其部分爲某些事情(如本例中的按鈕),你可以做這樣的事情:

 <style name="whiteText" parent="@android:style/TextAppearance.Medium"> 
      <item name="android:textStyle">normal</item> 
      <item name="android:textColor">@android:color/white</item> 
     </style> 
     <style name="buttonStyle" parent="@android:style/Widget.Button"> 
      <item name="android:textAppearance">@style/whiteText</item> 
      <item name="android:textStyle">bold</item> 
      <item name="android:textColor">@android:color/black</item> 
      <item name="android:gravity">center</item> 
      <item name="android:background">@drawable/pinkbutton</item> 
      <item name="android:cacheColorHint">@android:color/transparent</item> 
      <item name="android:minHeight">50.0dip</item> 
     </style> 
     <style name="DarkTheme" parent="@android:style/Theme.Black.NoTitleBar"> 
      <item name="android:buttonStyle">@style/buttonStyle</item> 
      <item name="android:textAppearance">@style/whiteText</item> 
     </style> 

可能是你唯一最好的地方,讓雖然起步將是:http://developer.android.com/design/style/index.html

相關問題