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

或者用另一种方式表达,在HTML中我可以在body,div和table级别设置字体大小。我可以在布局(LinearLayout,TableLayout,ScrollView等)级别上做类似的事情吗?


4079
2017-12-05 01:17


起源



答案:


为所有人设置全局样式 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">
  ...

15
2017-12-05 01:38



谢谢你的伎俩。我用谷歌搜索了几个小时,找不到它。再次感谢。 - k_man
甜蜜,很乐意提供帮助! - Roman Nurik
另外值得一提的是“你应该更喜欢sp(与比例无关的像素)来定义文本大小.sp比例因子取决于用户设置,系统缩放的大小与dp相同。” developer.android.com/guide/practices/... - scottyab
最后!我们的文字风格很痛苦,因为textAppearance不能正确地继承/级联(groups.google.com/d/msg/android-developers/KJfM0IhFRrk/...)。使用此属性可以解决问题。 - Walt Armour
几个小时后,我意识到这个答案不是我想要的。使用textViewStyle将设置属性,但也会阻止对这些值的本地覆盖(通过直接在TextView上设置textAppearance)。真正的解决方案:不要在你的主题上设置android:textAppearance,设置android:textAppearanceSmall!内部使用后一个属性来设置默认子视图的样式。这样也允许使用android:textAppearance对样式进行局部覆盖。 - Walt Armour


答案:


为所有人设置全局样式 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">
  ...

15
2017-12-05 01:38



谢谢你的伎俩。我用谷歌搜索了几个小时,找不到它。再次感谢。 - k_man
甜蜜,很乐意提供帮助! - Roman Nurik
另外值得一提的是“你应该更喜欢sp(与比例无关的像素)来定义文本大小.sp比例因子取决于用户设置,系统缩放的大小与dp相同。” developer.android.com/guide/practices/... - scottyab
最后!我们的文字风格很痛苦,因为textAppearance不能正确地继承/级联(groups.google.com/d/msg/android-developers/KJfM0IhFRrk/...)。使用此属性可以解决问题。 - Walt Armour
几个小时后,我意识到这个答案不是我想要的。使用textViewStyle将设置属性,但也会阻止对这些值的本地覆盖(通过直接在TextView上设置textAppearance)。真正的解决方案:不要在你的主题上设置android:textAppearance,设置android:textAppearanceSmall!内部使用后一个属性来设置默认子视图的样式。这样也允许使用android:textAppearance对样式进行局部覆盖。 - Walt Armour


或者,如果您想设置默认的全文文本样式但是为某些事情覆盖部分内容(例如本例中的按钮),您可以执行以下操作:

       <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


0
2018-06-01 12:44