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编程方式设置属性?
3 回答
![?](http://img1.sycdn.imooc.com/545863aa00014aa802200220-100-100.jpg)
慕勒3428872
TA贡献1848条经验 获得超6个赞
从技术上讲,您可以使用自定义视图以编程方式应用样式:
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));
![?](http://img1.sycdn.imooc.com/5333a0aa000121d702000200-100-100.jpg)
SMILET
TA贡献1796条经验 获得超4个赞
更新:在回答此问题时(2012年中,API级别14-15),以编程方式设置视图不是一种选择(即使存在一些非常重要的变通方法),而这是在更新的API之后实现的。版本。有关详细信息,请参阅@ Blundell的答案。
老答案:
您无法以编程方式设置视图的样式,但您可能会发现此线程很有用。
- 3 回答
- 0 关注
- 358 浏览
添加回答
举报
0/150
提交
取消