为 Android 中的视图设置全局样式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3078081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting global styles for Views in Android
提问by Emanuil Rusev
Let's say I want all the TextView
instances in my app to have textColor="#ffffff"
. Is there a way to set that in one place instead of setting it for each TextView
?
假设我希望TextView
我的应用程序中的所有实例都具有textColor="#ffffff"
. 有没有办法在一个地方设置它而不是为每个设置它TextView
?
回答by Steve Pomeroy
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.
实际上,您可以为 TextViews(和大多数其他内置小部件)设置默认样式,而无需执行自定义 java 类或单独设置样式。
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:
如果您查看themes.xml
Android 源代码,您将看到各种小部件的默认样式的一堆属性。关键是您在自定义主题中覆盖的textViewStyle
(或editTextStyle
等)属性。您可以通过以下方式覆盖这些:
Create a styles.xml
:
创建一个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
:
然后只需将该主题应用于您的应用程序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)!
并且您的所有文本视图都将默认为MyTextViewStyle
(在本例中为粗体和红色)中定义的样式!
This was tested on devices from API level 4 onward and seems to work great.
这已经在 API 级别 4 以后的设备上进行了测试,似乎效果很好。
回答by Cristian
There are two ways to do so:
有两种方法可以做到:
1. Using styles
1. 使用样式
You can define your own styles by creating XML files on the res/values
directory. So, let's suppose you want to have red and bold text, then you create a file with this content:
您可以通过在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>
You can name it like you want, for instance res/values/red.xml
. Then, the only thing you have to do is using that view in the widgets that you want, for instance:
您可以随意命名,例如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>
For further reference, you can read this article: Understanding Android Themes and Styles
如需进一步参考,您可以阅读这篇文章:Understanding Android Themes and Styles
2. Using custom classes
2. 使用自定义类
This is another possible way to achieve this, and it would be to provide your own TextView
that set the text color always to whatever you want; for instance:
这是实现此目的的另一种可能方法,它是提供您自己的方法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);
}
}
Then, you just have to treat it as a normal TextView
in your XML file:
然后,您只需要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>
Whether you use one or another choice depends on your needs. If the only thing you want to do is modifying the appearance, then the best approach is the first. On the other hand, if you want to change the appearance and add some new functionality to your widgets, the second one is the way to go.
使用一种还是另一种选择取决于您的需要。如果您只想修改外观,那么最好的方法是第一种。另一方面,如果您想更改外观并为小部件添加一些新功能,则第二个是可行的方法。
回答by hidro
For default text color in TextView
, set android:textColorTertiary
in your theme to desired color:
对于 中的默认文本颜色TextView
,请将android:textColorTertiary
您的主题设置为所需的颜色:
<item name="android:textColorTertiary">@color/your_text_color</item>
Many other Android control's colors can be controlled using framework attributes, or support library attributes if you use support library.
许多其他 Android 控件的颜色可以使用框架属性进行控制,如果您使用支持库,则可以使用支持库属性。
For a list of attributes you can set, check out Android source codeof styles.xml
and themes.xml
, or this very helpful gistby Dan Lew, try changing each value and see what they change on screen.
对于您可以设置属性的列表,请查看Android源代码的styles.xml
和themes.xml
,或者这个非常有用的要点丹卢,尝试改变每个值,看看它们改变屏幕上的内容。
回答by TWH
Define a style and use it on each widget, define a theme that overrides the android default for that widget, or define a string resource and reference it in each widget
定义一个样式并在每个小部件上使用它,定义一个覆盖该小部件的 android 默认值的主题,或者定义一个字符串资源并在每个小部件中引用它