Android:以编程方式设置视图样式 [英] Android: set view style programmatically
本文介绍了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
属性?
How to set style
attribute programmatically?
推荐答案
从技术上讲,您可以通过编程方式应用样式,并且始终使用自定义视图:
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.
因此,将此构造函数链接到带有样式参数的super.
So chain this constructor to the super that takes a style parameter.
RelativeLayout someLayout = new MyRelativeLayout(new ContextThemeWrapper(this,R.style.RadioButton));
或者就像@Dori所简单指出的那样:
Or as @Dori pointed out simply:
RelativeLayout someLayout = new RelativeLayout(new ContextThemeWrapper(activity,R.style.LightStyle));
这篇关于Android:以编程方式设置视图样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文