我正在尝试使用带有样式的 TextView
构造函数,就像这样:
TextView myText = new TextView(MyActivity.this, null, R.style.my_style);
不过,当我这样做时,文本视图似乎没有采用样式(我通过在静态对象上设置样式来验证了这一点)。
我还尝试使用myText.setTextAppearance(MyActivity.this, R.style.my_style)
,但它也无效。
我正在尝试使用带有样式的 TextView
构造函数,就像这样:
TextView myText = new TextView(MyActivity.this, null, R.style.my_style);
不过,当我这样做时,文本视图似乎没有采用样式(我通过在静态对象上设置样式来验证了这一点)。
我还尝试使用myText.setTextAppearance(MyActivity.this, R.style.my_style)
,但它也无效。
我认为你无法通过编程方式设置样式。为了解决这个问题,您可以创建一个模板布局xml文件,并分配样式。例如,在res/layout中创建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" />
然后通过inflate方法实例化您的新TextView:
TextView myText = (TextView)getLayoutInflater().inflate(R.layout.tvtemplate, null);
你可以创建一个通用的样式并在多个文本视图上重复使用,就像下面这个例子:
textView.setTextAppearance(this, R.style.MyTextStyle);
编辑: this
指的是 Context
对象。
TextViewCompat.setTextAppearance
。 - Rubin YooTextView myText = new TextView(new ContextThemeWrapper(MyActivity.this, R.style.my_style));
<style name="TabButton"><item name="android:background">@drawable/tabbar_button_bkgnd</item>...</style>
。然后我使用以下构造函数调用它:new Button(new ContextThemeWrapper(context, R.style.TabButton));
。但是按钮完全忽略了我的设置。我在这里做错了什么吗? - Aleks N.parent="@android:style/Widget.Button"
。 - maxcannaparent
没有改变任何东西。所以我选择了使用 inflate
解决方案。 - Aleks N.new Button(new ContextThemeWrapper(context, R.style.TabButton), null, 0)
。否则,将应用默认的按钮样式,这个样式会覆盖你通过ContextThemeWrapper合并到主题中的按钮样式。 - Newtonx您可以在构造函数中设置样式(但样式无法动态更改/设置)。
View(Context, AttributeSet, int)
(int
是当前主题中包含引用样式的属性)
参数 int defStyleAttr
没有指定样式。来自 Android 文档:
defStyleAttr - 当前主题中包含对样式资源的引用,该样式资源提供视图默认值的属性。可以为 0,以不查找默认值。
在 View 构造函数中设置样式,我们有两种可能的解决方案:
使用 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);
两种解决方案的关键 - defStyleAttr
参数应该设为0,才能将我们的样式应用于视图。
我也遇到了这个问题,并找到了通过程序设置样式的方法。也许大家都需要它,因此我在此更新。
View构造函数的第三个参数接受你的主题中attr的类型作为源代码如下:
public TextView(Context context, AttributeSet attrs) {
this(context, attrs, com.android.internal.R.attr.textViewStyle);
}
所以你必须使用 R.attr.** 而不是 R.style.** 这种类型。
在我的代码中,我执行了以下步骤:
首先,在 attr.xml 中自定义一个用于主题的定制化属性。
<attr name="radio_button_style" format="reference" />
其次,在 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>
RadioButton radioButton = new RadioButton(mContext, null, R.attr.radio_button_style);
当使用可能使用样式继承(甚至可定制的属性)的自定义视图时,您需要修改第二个构造函数以避免丢失样式。这对我有用,无需使用setTextAppearence():
public CustomView(Context context, AttributeSet attrs) {
this(context, attrs, attrs.getStyleAttribute());
}
inflate()
方法。android:layout_*
参数都不会被应用。null
作为ViewGroup parent
传递了。View view = inflater.inflate(R.layout.view, parent, false);
而parent
是ViewGroup
,从哪里想要调整android:layout_*
。