Android:以编程方式设置视图样式

Jim*_*Jim 202 android android-layout

这是XML:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/LightStyle"
    android:layout_width="fill_parent"
    android:layout_height="55dip"
    android:clickable="true"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" />

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

如何以style编程方式设置属性?

Blu*_*ell 196

从技术上讲,您可以使用自定义视图以编程方式应用样式:

private MyRelativeLayout extends RelativeLayout {
  public MyRelativeLayout(Context context) {
     super(context, null, R.style.LightStyle);
  }
}
Run Code Online (Sandbox Code Playgroud)

一个参数构造函数是以编程方式实例化视图时使用的构造函数.

因此,将此构造函数链接到带有样式参数的super.

RelativeLayout someLayout = new MyRelativeLayout(new ContextThemeWrapper(this,R.style.RadioButton));
Run Code Online (Sandbox Code Playgroud)

或者@Dori指出:

RelativeLayout someLayout = new RelativeLayout(new ContextThemeWrapper(activity,R.style.LightStyle));
Run Code Online (Sandbox Code Playgroud)

  • 您可以以编程方式执行此操作而无需扩展,因为3 arg构造函数无论如何都是公共的http://developer.android.com/reference/android/widget/RelativeLayout.html#RelativeLayout(android.content.Context,android.util.AttributeSet, INT) (31认同)
  • 如果是TextView,则需要对影响文本的属性(大小,颜色等)使用setTextAppearance(R.style.small_text) (8认同)
  • @Dori你会为`AttributeSet`传递什么? (2认同)
  • 我不知道为什么第三个参数方法不起作用.我申请了`Button`.@Benjamin Piette的做法很好. (2认同)

Ben*_*tte 109

什么对我有用:

Button b = new Button(new ContextThemeWrapper(this, R.style.ButtonText), null, 0);
Run Code Online (Sandbox Code Playgroud)
  • 使用ContextThemeWrapper

  • 使用3参数构造函数(没有这个将无法工作)

  • **重要!**此方法*可以*工作,但如果使用ContextThemeWrapper创建新的布局,也会为每个**子视图设置样式. (8认同)
  • @Gilian 确切地说,如果您在具有一个或多个子项的复合视图上使用它,它们将获得与您为其设置样式的父视图相同的样式。这可能会导致内部视图中的填充或边距过多,如果您不知道,可能会让您发疯:) (2认同)

Kor*_*urk 86

无法以编程方式设置视图的样式,但您可能会发现此线程很有用.

更新:在回答此问题时(2012年中,API级别14-15),以编程方式设置视图不是一种选择(即使存在一些非常重要的变通方法),而这是在更新的API之后实现的.版本.有关详细信息,请参阅@ Blundell的答案.

  • 对于某些情况(例如使用TextView),您可以使用`textView.setTextAppearance(context,R.style.mystyle);`.根据文档"从指定的TextAppearance资源设置文本颜色,大小,样式,提示颜色和突出显示颜色." http://developer.android.com/reference/android/widget/TextView.html#setTextAppearance(android.content.Context,int) (30认同)
  • 不对.请参阅@ Blundell的回答. (16认同)
  • api&gt; 23; textView.setTextAppearance(R.style.mystyle); (4认同)
  • @KorhanOzturk表示同意,但在SO上有这样的问题,有问题应该重新开放,接受新答案 (3认同)

Alo*_*gan 14

对于新的Button/TextView:

Button mMyButton = new Button(new ContextThemeWrapper(this, R.style.button_disabled), null, 0);
Run Code Online (Sandbox Code Playgroud)

对于现有实例:

mMyButton.setTextAppearance(this, R.style.button_enabled);
Run Code Online (Sandbox Code Playgroud)

对于图像或布局:

Image mMyImage = new ImageView(new ContextThemeWrapper(context, R.style.article_image), null, 0);
Run Code Online (Sandbox Code Playgroud)


con*_*ull 13

这是一个很老的问题,但现在对我有用的解决方案是使用构造函数的第四个参数defStyleRes- 如果可用......在视图中......设置样式

以下适用于我的目的(kotlin):

val textView = TextView(context, null, 0, R.style.Headline1)
Run Code Online (Sandbox Code Playgroud)

  • 请注意,[此构造函数](https://developer.android.com/reference/android/widget/TextView?hl=en#TextView(android.content.Context,%20android.util.AttributeSet,%20int,%20int) )已添加到 API 21 中,因此如果您的 `minSdkVersion` &lt; 21,您将收到错误“调用需要 API 级别 21(当前最小值为 19)” (3认同)

FOM*_*per 7

您可以通过执行以下操作将样式应用于您的活动:

super.setTheme( R.style.MyAppTheme );
Run Code Online (Sandbox Code Playgroud)

或Android默认值:

super.setTheme( android.R.style.Theme );
Run Code Online (Sandbox Code Playgroud)

在你的活动中,之前setContentView().

  • @siemian你能让FOM自由表达自己吗?他不需要编辑! (4认同)

Nat*_*ael 7

如果您想继续使用XML(接受的答案不允许您这样做)并在创建视图后设置样式,则可以使用支持所有可用属性子集的Paris库。

由于要从XML扩展视图,因此需要在布局中指定一个ID:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/my_styleable_relative_layout"
    style="@style/LightStyle"
    ...
Run Code Online (Sandbox Code Playgroud)

然后,当需要膨胀版面之后,当需要以编程方式更改样式时:

// Any way to get the view instance will do
RelativeLayout myView = findViewById(R.id.my_styleable_relative_layout);

// This will apply all the supported attribute values of the style
Paris.style(myView).apply(R.style.LightStyle);
Run Code Online (Sandbox Code Playgroud)

有关更多信息:支持的视图类型和属性的列表(包括背景,填充,边距等,可以轻松扩展)以及带有附加文档的安装说明

免责声明:我是上述图书馆的原作者。


Def*_*era 5

提供的答案均不正确。

您可以通过编程设置样式。

简短的答案是看看http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.1_r1/android/content/Context.java#435

长答案。这是我的片段,可通过编程方式为您的视图设置自定义样式:

1)在您的styles.xml文件中创建样式

 <style name="MyStyle">
    <item name="customTextColor">#39445B</item>
    <item name="customDividerColor">#8D5AA8</item>
</style>
Run Code Online (Sandbox Code Playgroud)

不要忘记在attrs.xml文件中定义您的自定义属性

我的attrsl.xml文件:

<declare-styleable name="CustomWidget">
    <attr name="customTextColor" format="color" />
    <attr name="customDividerColor" format="color" />
</declare-styleable>
Run Code Online (Sandbox Code Playgroud)

请注意,您可以为您的样式设置使用任何名称(我的CustomWidget)

现在,让我们将样式设置为小部件,这是我的简单小部件:

public class StyleableWidget extends LinearLayout {

private final StyleLoader styleLoader = new StyleLoader();

private TextView textView;
private View divider;

public StyleableWidget(Context context) {
    super(context);
    init();
}

private void init() {
    inflate(getContext(), R.layout.widget_styleable, this);
    textView = (TextView) findViewById(R.id.text_view);
    divider = findViewById(R.id.divider);
    setOrientation(VERTICAL);
}

protected void apply(StyleLoader.StyleAttrs styleAttrs) {
    textView.setTextColor(styleAttrs.textColor);
    divider.setBackgroundColor(styleAttrs.dividerColor);
}

public void setStyle(@StyleRes int style) {
    apply(styleLoader.load(getContext(), style));
}
}
Run Code Online (Sandbox Code Playgroud)

布局:

<TextView
    android:id="@+id/text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="22sp"
    android:layout_gravity="center"
    android:text="@string/styleble_title" />

<View
    android:id="@+id/divider"
    android:layout_width="match_parent"
    android:layout_height="1dp"/>

</merge>
Run Code Online (Sandbox Code Playgroud)

最后是StyleLoader类的实现

public class StyleLoader {

public StyleLoader() {

}

public static class StyleAttrs {
    public int textColor;
    public int dividerColor;
}

public StyleAttrs load(Context context, @StyleRes int styleResId) {
    final TypedArray styledAttributes = context.obtainStyledAttributes(styleResId, R.styleable.CustomWidget);
    return load(styledAttributes);
}

@NonNull
private StyleAttrs load(TypedArray styledAttributes) {
    StyleAttrs styleAttrs = new StyleAttrs();
    try {
        styleAttrs.textColor = styledAttributes.getColor(R.styleable.CustomWidget_customTextColor, 0);
        styleAttrs.dividerColor = styledAttributes.getColor(R.styleable.CustomWidget_customDividerColor, 0);
    } finally {
        styledAttributes.recycle();
    }
    return styleAttrs;
}
}
Run Code Online (Sandbox Code Playgroud)

您可以在https://github.com/Defuera/SetStylableProgramatically找到完整的示例

  • 这不是真正的样式,只是在视图(这里只有2个固定视图-*和*您肯定需要使用事先了解他们)。所有的魔力都在“ apply”方法中,这没什么有趣的。样式的真正含义是它会在所有以后动态添加的视图中自动应用某种视觉样式。当然,这里的代码无法做到这一点,这里没有动态,我们需要知道***哪些视图和***哪些属性/字段要设置。 (12认同)
  • 我不介意指定将样式应用于哪些视图,但此解决方案具有硬编码的样式元素(例如 textColor 和dividerColor)。它不会动态查找样式中定义的所有元素并将它们应用到视图中。 (2认同)

guo*_*ngj 5

这是我的简单例子,关键是ContextThemeWrapperwrapper,没有它,我的风格不行,使用View的三个参数构造函数。

ContextThemeWrapper themeContext = new ContextThemeWrapper(this, R.style.DefaultLabelStyle);
TextView tv = new TextView(themeContext, null, 0);
tv.setText("blah blah ...");
layout.addView(tv);
Run Code Online (Sandbox Code Playgroud)