假设我希望我的应用程序中所有的TextView
实例都具有textColor="#ffffff"
,是否有一种方法可以在一个地方设置它,而不是为每个TextView
单独设置?
假设我希望我的应用程序中所有的TextView
实例都具有textColor="#ffffff"
,是否有一种方法可以在一个地方设置它,而不是为每个TextView
单独设置?
themes.xml
,您将看到各种小部件的默认样式的一堆属性。关键是textViewStyle
(或editTextStyle
等)属性,您可以在自定义主题中覆盖它们。您可以按以下方式覆盖这些内容:创建一个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>
然后只需在AndroidManifest.xml
中将该主题应用于您的应用程序:
<application […] android:theme="@style/MyTheme">…
你的所有文本视图都将默认使用MyTextViewStyle
中定义的样式(在此示例中为粗体和红色)!
这已在API级别4及以上的设备上进行了测试,看起来效果非常好。
<item name="android:textViewStyle">@style/MyTextViewStyle</item>
,然后将该样式应用于我的应用程序,所有的TextView
都将按照该样式显示(甚至是操作栏中应用程序的名称)。如何解决操作栏的问题?我希望它具有默认外观。Github - Maksim Dmitriev<item name="android:textViewStyle">@style/MyTextViewStyle</item>
,指向ActionBar默认样式(或类似的东西)。 - Steve Pomeroyapplication
标签中为另一个小部件定义了样式,那该怎么办? style
属性似乎不接受多个值。 - jack_the_beast有两种方法可以实现:
您可以通过在res/values
目录中创建XML文件来定义自己的样式。假设您想要红色和粗体文本,则可以创建一个包含以下内容的文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyRedTheme" parent="android:Theme.Light">
<item name="android:textAppearance">@style/MyRedTextAppearance</item>
</style>
<style name="MyRedTextAppearance" parent="@android:style/TextAppearance">
<item name="android:textColor">#F00</item>
<item name="android:textStyle">bold</item>
</style>
</resources>
res/values/red.xml
。然后,在你想要使用该视图的部件中使用它即可,例如:<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
style="@style/MyRedTheme"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This is red, isn't it?"
/>
</LinearLayout>
为了更深入的了解,您可以阅读这篇文章:理解Android主题和样式
这是另一种可能的方法,就是提供自己的TextView
,始终将文本颜色设置为您想要的颜色;例如:
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.widget.TextView;
public class RedTextView extends TextView{
public RedTextView(Context context, AttributeSet attrs) {
super(context, attrs);
setTextColor(Color.RED);
}
}
接下来,您只需将其视为普通的TextView
在您的XML文件中:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<org.example.RedTextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This is red, isn't it?"
/>
</LinearLayout>
你使用哪种方法取决于你的需求。如果你只想修改外观,那么最好的方法是第一种。另一方面,如果你想改变外观并为小部件添加一些新功能,则第二种方法是正确的选择。
要设置TextView
中的默认文本颜色,请在您的主题中将android:textColorTertiary
设置为所需的颜色:
<item name="android:textColorTertiary">@color/your_text_color</item>
styles.xml
和themes.xml
的Android源代码,或者通过Dan Lew的这个非常有用的gist,尝试更改每个值并查看它们在屏幕上的变化。定义一个样式并在每个小部件上使用它,定义一个主题来覆盖 Android 默认的小部件样式,或者定义一个字符串资源并在每个小部件中引用它。