265

这是 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>

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

4

16 回答 16

253

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

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

单参数构造函数是在以编程方式实例化视图时使用的构造函数。

因此,将此构造函数链接到采用样式参数的 super。

RelativeLayout someLayout = new MyRelativeLayout(new ContextThemeWrapper(this,R.style.RadioButton));

或者正如@Dori 简单指出的那样:

RelativeLayout someLayout = new RelativeLayout(new ContextThemeWrapper(activity,R.style.LightStyle));

现在在 Kotlin 中:

class MyRelativeLayout @JvmOverloads constructor(
    context: Context, 
    attributeSet: AttributeSet? = null, 
    defStyleAttr: Int = R.style.LightStyle,
) : RelativeLayout(context, attributeSet, defStyleAttr)

或者

 val rl = RelativeLayout(ContextThemeWrapper(activity, R.style.LightStyle))
于 2014-01-10T11:38:10.523 回答
144

什么对我有用:

Button b = new Button(new ContextThemeWrapper(this, R.style.ButtonText), null, 0);
  • 使用 ContextThemeWrapper

  • 使用 3-arguments 构造函数(没有这个将无法工作)
于 2015-02-19T17:35:08.303 回答
93

更新:在回答这个问题时(2012 年年中,API 级别 14-15),以编程方式设置视图不是一种选择(即使有一些重要的解决方法),而在更新的 API 之后,这已经成为可能发布。有关详细信息,请参阅@Blundell 的答案。

老答案:

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

于 2012-07-30T14:54:31.710 回答
23

对于新的 Button/TextView:

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

对于现有实例:

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

对于图像或布局:

Image mMyImage = new ImageView(new ContextThemeWrapper(context, R.style.article_image), null, 0);
于 2018-06-10T16:10:59.577 回答
13

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

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

val textView = TextView(context, null, 0, R.style.Headline1)
于 2019-11-28T15:18:18.973 回答
11

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

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

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

然后,当您需要以编程方式更改样式时,在布局膨胀后:

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

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

免责声明:我是该库的原作者。

于 2018-04-16T03:59:08.097 回答
7

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

super.setTheme( R.style.MyAppTheme );

或安卓默认:

super.setTheme( android.R.style.Theme );

在您的活动中,之前setContentView()

于 2014-10-28T10:00:23.737 回答
6

提供的答案都不正确。

您可以以编程方式设置样式。

简短的回答是看看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>

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

我的 attrsl.xml 文件:

<declare-styleable name="CustomWidget">
    <attr name="customTextColor" format="color" />
    <attr name="customDividerColor" format="color" />
</declare-styleable>

请注意,您可以为您的样式使用任何名称(我的 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));
}
}

布局:

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

最后是 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;
}
}

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

于 2015-12-22T10:49:15.073 回答
6

这是我的简单例子,关键是ContextThemeWrapper包装器,没有它,我的样式不起作用,并且使用View的三参数构造函数。

ContextThemeWrapper themeContext = new ContextThemeWrapper(this, R.style.DefaultLabelStyle);
TextView tv = new TextView(themeContext, null, 0);
tv.setText("blah blah ...");
layout.addView(tv);
于 2018-06-05T01:31:18.523 回答
3

简单的方法是通过构造函数

RadioButton radioButton = new RadioButton(this,null,R.style.radiobutton_material_quiz);
于 2019-01-08T06:16:14.237 回答
2

我不建议使用 ContextThemeWrapper 这样做:

指定的主题将应用在基本上下文的主题之上。

什么会在您的应用程序中产生不需要的结果。相反,我向 Airbnb 的工程师提出了新的图书馆“巴黎”:

https://github.com/airbnb/paris

以编程方式定义和应用样式到 Android 视图。

但是在使用了一段时间后,我发现它实际上非常有限,我停止使用它,因为它不支持很多我需要开箱即用的属性,所以必须像往常一样检查并决定。

于 2018-11-13T18:47:11.717 回答
0

我发现使用带有自定义布局的 alertDialog 的最佳简单解决方案是:

val mView = LayoutInflater.from(context).inflate(layoutResId, null)

val dialog = AlertDialog.Builder(context, R.style.CustomAlertDialog)
    .setView(mView)
    .setCancelable(false)
    .create()

风格在哪里

<style name="CustomAlertDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="android:background">@drawable/bg_dialog_white_rounded</item>
</style>

和 bg_dialog_white_rounded.xml 是

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="16dp" />

    <solid android:color="@Color/white" />
</shape>

layoutResId 是必须将主题设置为“@style/CustomAlertDialog”的任何布局的资源 ID,例如:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginStart="@dimen/wdd_margin_medium"
    android:theme="@style/CustomAlertDialog"
    android:layout_marginEnd="@dimen/wdd_margin_medium">

..... etc...
</androidx.constraintlayout.widget.ConstraintLayout>
于 2021-09-10T14:53:25.793 回答
0
int buttonStyle = R.style.your_button_style;
Button button = new Button(new ContextThemeWrapper(context, buttonStyle), null, buttonStyle);

只有这个答案对我有用。见https://stackoverflow.com/a/24438579/5093308

于 2022-02-16T14:50:50.950 回答
-1

我在复合 ViewGroup 中使用 XML 中定义的视图,将它们膨胀添加到 Viewgroup。这样我就不能动态更改样式,但可以进行一些样式自定义。我的复合:

public class CalendarView extends LinearLayout {

private GridView mCalendarGrid;
private LinearLayout mActiveCalendars;

private CalendarAdapter calendarAdapter;

public CalendarView(Context context) {
    super(context);

}

public CalendarView(Context context, AttributeSet attrs) {
    super(context, attrs);

}

@Override
protected void onFinishInflate() {
    super.onFinishInflate();
    init();
}

private void init() {
    mCalendarGrid = (GridView) findViewById(R.id.calendarContents);
    mCalendarGrid.setNumColumns(CalendarAdapter.NUM_COLS);

    calendarAdapter = new CalendarAdapter(getContext());
    mCalendarGrid.setAdapter(calendarAdapter);
    mActiveCalendars = (LinearLayout) findViewById(R.id.calendarFooter);
}

}

和我在xml中的视图,我可以在其中分配样式:

<com.mfitbs.android.calendar.CalendarView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/calendar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="vertical"
>

<GridView
    android:id="@+id/calendarContents"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<LinearLayout
    android:id="@+id/calendarFooter"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    />

于 2014-09-08T11:27:58.023 回答
-1

如果在自己的自定义视图中: val editText = TextInputEditText(context, attrs, defStyleAttr)

于 2020-12-14T18:08:14.717 回答
-6

您可以创建包含所需样式的布局的 xml,然后更改视图的背景资源,如下所示

于 2012-07-30T14:58:55.680 回答