为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

杨__羊羊 2019-09-06 11:06:26
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 回答

?
慕勒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));




查看完整回答
反对 回复 2019-09-09
?
SMILET

TA贡献1796条经验 获得超4个赞

更新:在回答此问题时(2012年中,API级别14-15),以编程方式设置视图不是一种选择(即使存在一些非常重要的变通方法),而这是在更新的API之后实现的。版本。有关详细信息,请参阅@ Blundell的答案。

老答案:

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


查看完整回答
反对 回复 2019-09-09
  • 3 回答
  • 0 关注
  • 358 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信