android在代码中设置样式

时间:2010-06-29 15:13:38

标签: android coding-style textview

我正在尝试使用类似这样的样式的TextView构造函数:

TextView myText = new TextView(MyActivity.this, null, R.style.my_style );

然而,当我这样做时,文本视图似乎不采用样式(我通过在静态对象上设置样式来验证样式)。

我也尝试使用myText.setTextAppearance(MyActivity.this, R.style.my_style),但它也不起作用

12 个答案:

答案 0 :(得分:308)

我不相信你可以以编程方式设置样式。要解决此问题,您可以创建一个模板布局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" />

然后膨胀它以实例化你的新TextView:

TextView myText = (TextView)getLayoutInflater().inflate(R.layout.tvtemplate, null);

希望这有帮助。

答案 1 :(得分:116)

您可以创建一个通用样式,并在多个文本视图上重复使用它,如下所示:

textView.setTextAppearance(this, R.style.MyTextStyle);

修改:指上下文

答案 2 :(得分:92)

您可以将ContextThemeWrapper传递给构造函数,如下所示:

TextView myText = new TextView(new ContextThemeWrapper(MyActivity.this, R.style.my_style));

答案 3 :(得分:16)

您可以在构造函数中设置样式(但不能动态更改/设置样式)。

View(Context, AttributeSet, int)int是一种样式资源)

Answer from Romain Guy

reference

答案 4 :(得分:8)

参数int defStyleAttr未指定样式。来自Android文档:

  

defStyleAttr - 当前主题中包含的属性   引用为其提供默认值的样式资源   视图。可以为0以查找默认值。

要在View构造函数中设置样式,我们有两种可能的解决方案:

  1. 使用ContextThemeWrapper:

    ContextThemeWrapper wrappedContext = new ContextThemeWrapper(yourContext, R.style.your_style);
    TextView textView = new TextView(wrappedContext, null, 0);
    
  2. 使用四参数构造函数(从LOLLIPOP开始):

    TextView textView = new TextView(yourContext, null, 0, R.style.your_style);
    
  3. 两个解决方案的

    关键字 - defStyleAttr参数应为0,以便将我们的样式应用于视图。

答案 5 :(得分:5)

(尚未)支持动态更改样式。您必须通过XML在创建视图之前设置样式。

答案 6 :(得分:2)

使用可能使用样式继承(或事件样式属性)的自定义视图时,必须修改第二个构造函数,以免丢失样式。这对我有用,无需使用 setTextAppearence()

public CustomView(Context context, AttributeSet attrs) {
    this(context, attrs, attrs.getStyleAttribute());
}

答案 7 :(得分:2)

接受的答案对我来说是一个很好的解决方案。唯一要添加的是约inflate()方法。

在接受的答案中,不会应用所有android:layout_*个参数。

原因是无法对其进行调整,因为null已作为ViewGroup parent传递。

你可以像这样使用它:

View view = inflater.inflate(R.layout.view, parent, false);

parentViewGroup,您可以从中调整android:layout_*

在这种情况下,将设置所有相关属性。

希望它对某人有用。

答案 8 :(得分:1)

我也遇到了这个问题,我找到了以编程方式设置样式的方法。也许你们都需要它,所以我在那里更新。

View构造函数的第三个参数接受主题中的attr类型作为下面的源代码:

public TextView(Context context, AttributeSet attrs) {
    this(context, attrs, com.android.internal.R.attr.textViewStyle);
}

所以你必须传递一种R.attr。**而不是R.style。**

在我的代码中,我执行了以下步骤:

首先,自定义attr.xml中主题使用的自定义attr。

<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);

以编程方式创建的视图将使用主题中指定的样式。

你可以尝试一下,希望它能完美地为你效用。

答案 9 :(得分:0)

我只使用EditText进行了测试,但您可以使用方法

  

public void setBackgroundResource(int resid)

应用XML文件中定义的样式。

正弦此方法属于View我相信它适用于任何UI元素。

问候。

答案 10 :(得分:0)

我们可以使用TextViewCompact.setTextAppearance(textView, R.style.xyz)

Android doc供参考。

答案 11 :(得分:0)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) 
    textView.setTextAppearance(R.style.yourStyle)