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

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编程方式设置属性?


HUWWW
浏览 354回答 3
3回答

千万里不及你

从技术上讲,您可以使用自定义视图以编程方式应用样式:private&nbsp;MyRelativeLayout&nbsp;extends&nbsp;RelativeLayout&nbsp;{ &nbsp;&nbsp;public&nbsp;MyRelativeLayout(Context&nbsp;context)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;super(context,&nbsp;null,&nbsp;R.style.LightStyle); &nbsp;&nbsp;}}一个参数构造函数是以编程方式实例化视图时使用的构造函数。因此,将此构造函数链接到带有样式参数的super。RelativeLayout&nbsp;someLayout&nbsp;=&nbsp;new&nbsp;MyRelativeLayout(new&nbsp;ContextThemeWrapper(this,R.style.RadioButton));或者@Dori指出:RelativeLayout&nbsp;someLayout&nbsp;=&nbsp;new&nbsp;RelativeLayout(new&nbsp;ContextThemeWrapper(activity,R.style.LightStyle));

白猪掌柜的

更新:在回答此问题时(2012年中,API级别14-15),以编程方式设置视图不是一种选择(即使存在一些非常重要的变通方法),而这是在更新的API之后实现的。版本。有关详细信息,请参阅@ Blundell的答案。老答案:您无法以编程方式设置视图的样式,但您可能会发现此线程很有用。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android