Android 修改App中默认TextView的字体和颜色

来源:转载

一、别人怎么做

来源

http://stackoverflow.com/questions/3078081/setting-global-styles-for-views-in-android
Actually, you can set a default style for TextViews (and most other built-in widgets) without needing to do a custom java class or setting the style individually.

If you take a look in themes.xml in the Android source, you will see a bunch of attributes for the default style for various widgets. The key is the textViewStyle (or editTextStyle, etc.) attribute which you override in your custom theme. You can override these in the following way:

Create a styles.xml:

<?xml version="1.0" encoding="utf-8"?><resources> <style name="MyTheme" parent="android:Theme"> <item name="android:textViewStyle">@style/MyTextViewStyle</item> </style> <style name="MyTextViewStyle" parent="android:Widget.TextView"> <item name="android:textColor">#F00</item> <item name="android:textStyle">bold</item> </style></resources> 

Then just apply that theme to your application in AndroidManifest.xml:

<application […] android:theme="@style/MyTheme">…

And all your text views will default to the style defined in MyTextViewStyle (in this instance, bold and red)!

This was tested on devices from API level 4 onward and seems to work great.

结论

<style name="AppBaseTheme" parent="Base.Theme.AppCompat.Light"> <!-- Theme customizations available in newer API levels can go in res/values-vXX/styles.xml, while customizations related to backward-compatibility can go here. --> <!--<item>@color/main_background_color</item>--> <item name="android:windowNoTitle">true</item> <item name="android:screenOrientation">portrait</item> <item name="android:textViewStyle">@style/MyTextViewStyle</item> </style> <style name="MyTextViewStyle" parent="android:Widget.TextView"> <item name="android:textColor">@color/text_color_normal</item> <item name="android:textSize">14sp</item> </style>

测试可行

二、自测可行的另一种方式

 <item name="android:textColorPrimary">@color/text_color_default</item> <item name="android:textColorSecondary">@color/text_color_default</item> <item name="android:textColorTertiary">@color/text_color_default</item>

三、遇到的问题

使用过程中,发现这个全局style对ListView中的TextView不生效

解决方案

不适用getApplicationContext,改使用Activity

//adapter = new ShopResultAdapter(getApplicationContext(), datas);//替换为Activityadapter = new ShopResultAdapter(this, datas);


分享给朋友:
您可能感兴趣的文章:
随机阅读: