15

我正在尝试将 textsize 设置为全局级别,但无法确定或找到方法来做到这一点。

例如我有这样的事情:

<?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 的基本 textSize?”

或者换一种说法,在 HTML 中,我可以在 body、div 和 table 级别设置字体大小。我可以在一个布局(LinearLayout、TableLayout、ScrollView 等)级别上做一些类似的事情吗?

4

2 回答 2

17

TextView要为所有s设置全局样式,请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">
  ...
于 2011-12-05T01:38:55.580 回答
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

于 2012-06-01T12:44:13.523 回答