Android: How do I set the textsize for a layout?

吃可爱长大的小学妹 提交于 2019-12-06 18:42:57

问题


I am trying to set the textsize at a global level and cannot figure or find a way to do it.

For example I have something like this:

<?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>

The TableRows and TextViews themselves are generated dynamically depending on user input.

The problem is I would like to set the base textSize globally based on a resource file without having to go and touch a bunch of code. Is there a way to tell the app, "hey app, use this as your base textSize for all the textViews?"

Or phrased another way, in HTML I can set the font size at the body, div, and table levels. Is there something analogous I can do at one the layout (LinearLayout, TableLayout, ScrollView, etc.) levels?


回答1:


To set the global styling for all TextViews, set the android:textViewStyle attribute in a custom theme and apply the theme to your application or individual activities.

Pseudocode:

<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">
  ...



回答2:


Or if you wanted to set the default all around text style but override parts of it for certain things (like the button in this example) you could do something like this:

       <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>

Probably your single best place to get started though would be: http://developer.android.com/design/style/index.html



来源:https://stackoverflow.com/questions/8380020/android-how-do-i-set-the-textsize-for-a-layout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!