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

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


当前回答

更新:在回答这个问题的时候(2012年中期,API级别14-15),以编程方式设置视图还不是一个选项(即使有一些重要的变通方法),而在最近的API发布之后,这已经成为可能。详见@Blundell的回答。

旧的回答:

您还不能通过编程方式设置视图的样式,但是您可能会发现这个线程很有用。

其他回答

我发现最好的简单解决方案,使用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>

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

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)

or

 val rl = RelativeLayout(ContextThemeWrapper(activity, R.style.LightStyle))

我在我的复合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"
    />

这是一个相当老的问题,但解决方案,为我工作现在是使用构造函数defStyleRes的第4个参数-如果可用..视图…设置风格

以下工作为我的目的(kotlin):

val textView = TextView(context, null, 0, R.style.Headline1)

你可以通过以下方式将样式应用到你的活动:

super.setTheme( R.style.MyAppTheme );

或Android默认设置:

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

在setContentView()之前。