Android:在运行时设置 TextView 样式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3237752/
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
Android : set TextView style at runtime
提问by Mohammad Rukab
Does any body know how to set style for TextView
at run time:
有没有人知道如何TextView
在运行时设置样式:
something like this
像这样的东西
myTextView.setStyle(R.style.mystyle);
回答by Yayo28
Very easy just use setTextApparence and your style
非常简单,只需使用 setTextApparence 和您的样式
myTextView.setTextAppearance(getApplicationContext(), R.style.boldText);
回答by Ryan Conrad
you will have to manually set each element of the style that you change, there is no way to setStyle at run time, AFAIK.
您必须手动设置您更改的样式的每个元素,AFAIK 无法在运行时设置样式。
myTextView.setTextAppearance
myTextView.setTextSize
myTextView.setTextColor
回答by Vincent Mimoun-Prat
I also still did not find (sadly) a way to change Style at runtime.
我也仍然没有(遗憾地)找到一种在运行时更改样式的方法。
If it is just about changing the checkbox appearance (as you mention in a comment of another answer), you can use this:
如果只是改变复选框的外观(正如您在另一个答案的评论中提到的),您可以使用:
myCheckbox.setButtonDrawable(R.drawable.star_checkbox);
And have a star_checkbox.xml file in the drawable directory describing the checkbox background according to its states such as:
并在 drawable 目录中有一个 star_checkbox.xml 文件,根据其状态描述复选框背景,例如:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:state_focused="true"
android:drawable="@drawable/star_checkbox_checked_focused" />
<item android:state_checked="false" android:state_focused="true"
android:drawable="@drawable/checkbox_not_checked_focused" />
<item android:state_checked="false"
android:drawable="@drawable/checkbox_not_checked" />
<item android:state_checked="true"
android:drawable="@drawable/checkbox_checked" />
</selector>
Also you need the corresponding png files in you drawable directory.
您还需要 drawable 目录中的相应 png 文件。
回答by verylongword
I am trying myself to do a similar thing.
我正在尝试自己做类似的事情。
My reason is that I want to use a style from my own Theme, BUT my User Interface Layout is entirely generated in code( using a custom layout builder), without defining any widgets in XML. So I cannot set a style in the XML layout of my widget – there isn't any XML layout.
我的原因是我想使用我自己主题中的样式,但是我的用户界面布局完全是在代码中生成的(使用自定义布局构建器),而没有在 XML 中定义任何小部件。所以我不能在我的小部件的 XML 布局中设置样式——没有任何 XML 布局。
I am thinking that I will be able to set this style in the code of my widget by using
我想我将能够通过使用在我的小部件的代码中设置这种样式
TypedArray a =
TypedArray a =
context.obtainStyledAttributes(AttributeSet set, int[] attrs, int defStyleAttr, int defStyleRes)
context.obtainStyledAttributes(AttributeSet set, int[] attrs, int defStyleAttr, int defStyleRes)
Here it seems (to me) that
这似乎(对我来说)
AttributeSet set = null; because this is what the XML inflater would have provided.
int[] attrs = R.styleable.MyWidget; defines what attributes I want to look at.
int defStyleAttr = myWidgetStyle; which is a reference, defined in my Theme, to a style for MyWidget. These are both defined in XML files in res/values. “myWidgetStyle” follows the pattern of name the android developers have used in their code.
defStyleRes = 0; I am hoping that I don't need to think about this.
AttributeSet set = null; 因为这是 XML inflater 会提供的。
int[] attrs = R.styleable.MyWidget; 定义我想要查看的属性。
int defStyleAttr = myWidgetStyle; 这是在我的主题中定义的对 MyWidget 样式的引用。这些都在 res/values 中的 XML 文件中定义。“myWidgetStyle”遵循 android 开发人员在其代码中使用的名称模式。
defStyleRes = 0; 我希望我不需要考虑这个。
Then to get any property , such as a background color,
然后获取任何属性,例如背景颜色,
Color color = a.getColor(R.styleable.MyWidget_background, R.color.my_default);
Color color = a.getColor(R.styleable.MyWidget_background, R.color.my_default);
a.recycle();
a.recycle();
This does seem to work –so far anyway.
这似乎确实有效 - 无论如何。
It seems that the android build system conveniently generates the correct index to use in a.getColor, and names it R.styleable.MyWidget_background . I didn't make this name myself so Android must have done it using my XML for my styleable MyWidget.
似乎 android 构建系统可以方便地生成在 a.getColor 中使用的正确索引,并将其命名为 R.styleable.MyWidget_background 。这个名字不是我自己取的,所以 Android 一定是使用我的 XML 来为我的样式化 MyWidget 命名的。
I expect one can look up the correct index by searching the TypedArray for the required attribute , but that would be inefficient and the TypedArray looks like an unpleasant contraption to deal with. I would use a very long stick to poke it!
我希望人们可以通过在 TypedArray 中搜索所需的属性来查找正确的索引,但这会效率低下,而且 TypedArray 看起来像是一个令人不快的装置。我会用一根很长的棍子戳它!
Don
大学教师
回答by Pawan Maheshwari
TextView (Context context, AttributeSet attrs, int defStyle)
TextView(上下文上下文、AttributeSet attrs、int defStyle)
Although there is construction available but it seems its buggy, I tried this and found specified style won't apply on my view.
虽然有可用的构造,但它似乎有问题,我尝试了这个,发现指定的样式不适用于我的视图。
After searching further got this filed bug: http://code.google.com/p/android/issues/detail?id=12683
进一步搜索后得到了这个提交的错误:http: //code.google.com/p/android/issues/detail?id= 12683
To workaround this issue I am using setBackgroundResource, setTextAppearance, etc methods dramatically :)
为了解决这个问题,我戏剧性地使用了 setBackgroundResource、setTextAppearance 等方法:)