【问题标题】:Android: How do I set the textsize for a layout?Android:如何设置布局的文本大小?
【发布时间】:2012-01-12 21:08:30
【问题描述】:

我正在尝试将 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 ,而不必去接触一堆代码。有没有办法告诉应用程序,“嘿,应用程序,将它用作所有 textView 的基本 textSize?”

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

【问题讨论】:

    标签: android android-layout text-size


    【解决方案1】:

    要为所有TextViews 设置全局样式,请在自定义主题中设置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">
      ...
    

    【讨论】:

    • 谢谢你成功了。我用谷歌搜索了几个小时,找不到它。再次感谢。
    • 另外值得一提的是“您应该更喜欢 sp(与比例无关的像素)来定义文本大小。sp 比例因子取决于用户设置,系统缩放大小与它相同为 dp。” developer.android.com/guide/practices/…
    • 终于!由于 textAppearance 无法正确继承/级联(groups.google.com/d/msg/android-developers/KJfM0IhFRrk/…),我们已经为文本样式感到痛苦很久了。使用此属性可以解决问题。
    • 几个小时后,我意识到这个答案不是我想要的。使用 textViewStyle 将设置属性,但也会阻止这些值的本地覆盖(通过直接在 TextView 上设置 textAppearance)。真正的解决方案:不要在你的主题上设置 android:textAppearance,设置 android:textAppearanceSmall!内部使用后一个属性来设置默认子文本视图的样式。这也允许使用 android:textAppearance 本地覆盖样式。
    【解决方案2】:

    或者,如果你想设置默认的文本样式,但为了某些事情(比如这个例子中的按钮)覆盖它的一部分,你可以这样做:

           <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

    【讨论】:

      猜你喜欢
      • 2011-10-11
      • 2013-07-23
      • 2020-05-11
      • 2022-01-18
      • 2018-03-02
      • 1970-01-01
      • 1970-01-01
      • 2021-10-01
      • 1970-01-01
      相关资源
      最近更新 更多