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

这里是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属性?

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

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

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

所以把这个构造函数链接到需要一个样式参数的超级类。

  RelativeLayout someLayout = new MyRelativeLayout(context); 

或者@Dori简单地指出:

  RelativeLayout someLayout = new RelativeLayout(context, null, R.style.LightStyle); 

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

更新 :在回答这个问题的时候(2012年年中,API级别14-15),以编程方式设置视图不是一个选项(即使有一些不平凡的解决方法),而这已经在更新的API版本。 有关详细信息,请参阅@ Blundell的答案。

什么对我有效:

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

  • 使用3个参数的构造函数(不会没有这个工作)

您可以通过以下方式将风格应用于您的活动:

 super.setTheme( R.style.MyAppTheme ); 

或Android默认:

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

在你的activity中,在setContentView()之前。

非提供的答案是正确的。

您可以编程式设置样式。

简短的回答是看看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文件中创build一个样式

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

注意,你可以使用任何名字作为你的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/SetStylableProgramaticallyfind完整的工作示例;

我在我的复合ViewGroup中使用了XML中定义的视图,将它们添加到Viewgroup中。 这样我不能dynamic地改变样式,但我可以做一些样式自定义。 我的合成:

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

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