2011-12-05 48 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

相关问题