How to set text color in android app for all text?

后端 未结 5 836
执念已碎
执念已碎 2021-02-01 12:16

I want to define a default text color for my android app.

I have a base activity class, that all activities are extended from it and I thought this might be a good place

相关标签:
5条回答
  • 2021-02-01 12:48

    Create a custom theme for your app. Take look at the official guide.

    0 讨论(0)
  • 2021-02-01 12:50
    • Create style for TextView:

      <style name="TextViewTheme">
          <item name="android:textColor">@android:color/white</item>
      </style>
      
    • Apply it in style for App:

      <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
          <item name="colorPrimary">@color/colorPrimary</item>
          <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
          <item name="colorAccent">@color/colorAccent</item>
          <item name="android:textViewStyle">@style/TextViewTheme</item>
      </style>
      
    • And remember to change style in AndroidManifest.xml:

      <application
          android:icon="@mipmap/ic_launcher"
          android:label="@string/app_name"
          android:theme="@style/AppTheme" >
          ...
      </application>
      
    0 讨论(0)
  • 2021-02-01 12:54

    Set your default color in your res/values/colors.xml like this

    <color name="defaultTextColor">#ffffff</color>
    

    So this color to all your texts

    android:textColor="@color/defaultTextColor"
    

    or

    textView.setTextColor(R.color.defaultTextColor);
    
    0 讨论(0)
  • 2021-02-01 12:59

    As was mentioned in denis.solonenko's answer, the correct approach would be to modify your theme.

    Where you define your theme (in your themes.xml or styles.xml file), you'll want to add something like this:

    <style name="AppTheme" parent="@style/Theme.AppCompat.Light">
        ...
        <item name="android:textColor">#FF00FF</item>
        ...
    </style>
    

    Then make sure that the theme is applied to your Activity or Application in the manifest:

    <application
        ...
        android:theme="@style/AppTheme"
        .... 
        >
    

    You can also define:

    • textColor - The default text color of any given view
    • textColorPrimary - The default text color for enabled buttons and Large Textviews
    • textColorSecondary - The default text color for Medium and Small Textviews
    • textColorTertiary - ?

    (Source TextColor vs TextColorPrimary vs TextColorSecondary)

    Keep in mind that many other things may override these predefined colors, such as applied styles or definitions in different resource folders.

    See here for a full list of theme items: https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/res/res/values/themes.xml

    0 讨论(0)
  • 2021-02-01 13:02

    Yes you are right you can make that using style. Or you can use TextView.getTextColors().getDefaultColor() for set default text color. Actually I never used this but I think it may be help you.

    For style

    <style name="TextColor">
        <item name="android:textColor">#00FF00</item>
    </style>      
    

    Then in layout file

    <TextView  style="@style/TextColor" />
    
    0 讨论(0)
提交回复
热议问题