Android 以编程方式为 TextView 设置样式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3142067/
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
Set style for TextView programmatically
提问by Ben
I'm trying to use the TextView
constructor with style like this:
我正在尝试使用TextView
具有以下样式的构造函数:
TextView myText = new TextView(MyActivity.this, null, R.style.my_style);
However, when I do this, the text view does not appear to take the style (I verified the style by setting it on a static object).
但是,当我这样做时,文本视图似乎没有采用该样式(我通过在静态对象上设置样式来验证该样式)。
I've also tried using myText.setTextAppearance(MyActivity.this, R.style.my_style)
but it also doesn't work.
我也试过使用,myText.setTextAppearance(MyActivity.this, R.style.my_style)
但它也不起作用。
回答by Dan Kilpatrick
I do not believe you can set the style programatically. To get around this you can create a template layout xml file with the style assigned, for example in res/layout create tvtemplate.xml as with the following content:
我不相信您可以以编程方式设置样式。为了解决这个问题,您可以创建一个具有指定样式的模板布局 xml 文件,例如在 res/layout create tvtemplate.xml 中,内容如下:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This is a template"
style="@style/my_style" />
then inflate this to instantiate your new TextView:
然后将其膨胀以实例化您的新 TextView:
TextView myText = (TextView)getLayoutInflater().inflate(R.layout.tvtemplate, null);
Hope this helps.
希望这可以帮助。
回答by Shahul3D
You can create a generic style and re-use it on multiple textviews like the one below:
您可以创建一个通用样式并在多个文本视图上重复使用它,如下所示:
textView.setTextAppearance(this, R.style.MyTextStyle);
Edit: thisrefers to Context
编辑:这是指上下文
回答by maxcanna
You can pass a ContextThemeWrapperto the constructor like this:
您可以像这样将ContextThemeWrapper传递给构造函数:
TextView myText = new TextView(new ContextThemeWrapper(MyActivity.this, R.style.my_style));
回答by Dandre Allison
You can set the style in the constructor (but styles can not be dynamically changed/set).
您可以在构造函数中设置样式(但样式不能动态更改/设置)。
View(Context, AttributeSet, int)
(the int
is an attribute in the current theme that contains a reference to a style)
View(Context, AttributeSet, int)
(int
是当前主题中的一个属性,包含对样式的引用)
回答by DmitryArc
Parameter int defStyleAttr
does not specifies the style. From the Android documentation:
参数 intdefStyleAttr
不指定样式。从 Android 文档:
defStyleAttr- An attribute in the current theme that contains a reference to a style resource that supplies default values for the view. Can be 0 to not look for defaults.
defStyleAttr- 当前主题中的一个属性,包含对为视图提供默认值的样式资源的引用。可以为 0 以不查找默认值。
To setup the style in View constructor we have 2 possible solutions:
要在 View 构造函数中设置样式,我们有两种可能的解决方案:
With use of ContextThemeWrapper:
ContextThemeWrapper wrappedContext = new ContextThemeWrapper(yourContext, R.style.your_style); TextView textView = new TextView(wrappedContext, null, 0);
With four-argument constructor (available starting from LOLLIPOP):
TextView textView = new TextView(yourContext, null, 0, R.style.your_style);
使用 ContextThemeWrapper:
ContextThemeWrapper wrappedContext = new ContextThemeWrapper(yourContext, R.style.your_style); TextView textView = new TextView(wrappedContext, null, 0);
使用四参数构造函数(从 LOLLIPOP 开始可用):
TextView textView = new TextView(yourContext, null, 0, R.style.your_style);
Key thingfor both solutions - defStyleAttr
parameter should be 0 to apply our style to the view.
两种解决方案的关键-defStyleAttr
参数应为 0 以将我们的样式应用于视图。
回答by Chris Cashwell
Dynamically changing styles is not supported (yet). You have to set the style beforethe view gets created, via XML.
不支持动态更改样式(尚)。您必须在创建视图之前通过 XML设置样式。
回答by Anor
The accepted answer was great solution for me. The only thing to add is about inflate()
method.
接受的答案对我来说是很好的解决方案。唯一要添加的是inflate()
方法。
In accepted answer all android:layout_*
parameters will not be applied.
在接受的答案中,android:layout_*
不会应用所有参数。
The reason is no way to adjust it, cause null
was passed as ViewGroup parent
.
原因是没有办法调整它,原因null
被传递为ViewGroup parent
.
You can use it like this:
你可以这样使用它:
View view = inflater.inflate(R.layout.view, parent, false);
View view = inflater.inflate(R.layout.view, parent, false);
and the parent
is the ViewGroup
, from where you like to adjust android:layout_*
.
并且parent
是ViewGroup
,从您喜欢的地方调整android:layout_*
。
In this case, all relative properties will be set.
在这种情况下,将设置所有相关属性。
Hope it'll be useful for someone.
希望它对某人有用。
回答by mrtwo
I met the problem too, and I found the way to set style programatically. Maybe you all need it, So I update there.
我也遇到了这个问题,我找到了以编程方式设置样式的方法。也许你们都需要它,所以我在那里更新。
The third param of View constructor accepts a type of attr in your theme as the source code below:
View 构造函数的第三个参数接受主题中的一种 attr 作为以下源代码:
public TextView(Context context, AttributeSet attrs) {
this(context, attrs, com.android.internal.R.attr.textViewStyle);
}
So you must pass a type of R.attr.** rather than R.style.**
所以你必须传递一种 R.attr.** 而不是 R.style.**
In my codes, I did following steps:
在我的代码中,我做了以下步骤:
First, customize a customized attr to be used by themes in attr.xml.
首先,在 attr.xml 中自定义要由主题使用的自定义属性。
<attr name="radio_button_style" format="reference" />
Second, specific your style in your used theme in style.xml.
其次,在 style.xml 中使用的主题中指定您的风格。
<style name="AppTheme" parent="android:Theme.Translucent">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
<item name="radio_button_style">@style/radioButtonStyle</item>
</style>
<style name="radioButtonStyle" parent="@android:style/Widget.CompoundButton.RadioButton">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">64dp</item>
<item name="android:background">#000</item>
<item name="android:button">@null</item>
<item name="android:gravity">center</item>
<item name="android:saveEnabled">false</item>
<item name="android:textColor">@drawable/option_text_color</item>
<item name="android:textSize">9sp</item>
</style>
At the end, use it!
最后,使用它!
RadioButton radioButton = new RadioButton(mContext, null, R.attr.radio_button_style);
the view created programatically will use the specified style in your theme.
以编程方式创建的视图将在您的主题中使用指定的样式。
You can have a try, and hope it can work for you perfectly.
您可以尝试一下,希望它可以完美地为您服务。
回答by saiyancoder
When using custom views that may use style inheritance (or event styleable attributes), you have to modify the second constructor in order not to lose the style. This worked for me, without needing to use setTextAppearence():
当使用可能使用样式继承(或事件样式属性)的自定义视图时,您必须修改第二个构造函数以免丢失样式。这对我有用,无需使用setTextAppearence():
public CustomView(Context context, AttributeSet attrs) {
this(context, attrs, attrs.getStyleAttribute());
}
回答by floydaddict
I have only tested with EditText but you can use the method
我只用 EditText 测试过,但您可以使用该方法
public void setBackgroundResource (int resid)
public void setBackgroundResource (int resid)
to apply a style defined in an XML file.
应用在 XML 文件中定义的样式。
Sine this method belongs to View I believe it will work with any UI element.
由于此方法属于 View 我相信它适用于任何 UI 元素。
regards.
问候。