Android: set view style programmatically

2019-01-01 07:04发布

Here's 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>

How to set style attribute programmatically?

11条回答
看淡一切
2楼-- · 2019-01-01 07:24

You cannot set a view's style programmatically yet, but you may find this thread useful.

Update: At the time of answering this question (mid 2012, API level 14-15), setting the view programmatically was not an option (even though there were some non-trivial workarounds)whereas this has been made possible after the more recent API releases. See @Blundell's answer for details.

查看更多
千与千寻千般痛.
3楼-- · 2019-01-01 07:28

If you'd like to continue using XML (which the accepted answer doesn't let you do) and set the style after the view has been created you may be able to use the Paris library which supports a subset of all available attributes.

Since you're inflating your view from XML you'd need to specify an id in the layout:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/my_styleable_relative_layout"
    style="@style/LightStyle"
    ...

Then when you need to change the style programmatically, after the layout has been inflated:

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

For more: the list of supported view types and attributes (includes background, padding, margin, etc. and can easily be extended) and installation instructions with additional documentation.

Disclaimer: I'm the original author of said library.

查看更多
何处买醉
4楼-- · 2019-01-01 07:32

Technically you can apply styles programmatically, with custom views anyway:

private MyRelativeLayout extends RelativeLayout {
  public MyRelativeLayout(Context context) {
     super(context, null, R.style.LightStyle);
  }
}

The one argument constructor is the one used when you instantiate views programmatically.

So chain this constructor to the super that takes a style parameter.

RelativeLayout someLayout = new MyRelativeLayout(context);

Or as @Dori pointed out simply:

RelativeLayout someLayout = new RelativeLayout(context, null, R.style.LightStyle);
查看更多
步步皆殇っ
5楼-- · 2019-01-01 07:32

For a new Button/TextView:

Button mMyButton = new Button(new ContextThemeWrapper(this, R.style.button_disabled), null, 0);

For an existing instance:

mMyButton.setTextAppearance(this, R.style.button_enabled);

For Image or layouts:

Image mMyImage = new ImageView(new ContextThemeWrapper(context, R.style.article_image), null, 0);
查看更多
梦寄多情
6楼-- · 2019-01-01 07:33

I don't propose to use ContextThemeWrapper as it do this:

    The specified theme will be applied on top of 
    the base context's theme.

What can make unwanted results in your application. Instead I propose new library "paris" for this from airbnb guys:

https://github.com/airbnb/paris

Define and apply styles to Android views programmatically
查看更多
查无此人
7楼-- · 2019-01-01 07:34

This is my simple example, the key is the ContextThemeWrapper wrapper, without it, my style does not work, and using the three parameters constructor of the View.

ContextThemeWrapper themeContext = new ContextThemeWrapper(this, R.style.DefaultLabelStyle);
TextView tv = new TextView(themeContext, null, 0);
tv.setText("blah blah ...");
layout.addView(tv);
查看更多
登录 后发表回答