通过代码为 View 设置 style

Android 开发中写界面时,可以通过 xml 去配置 View 的属性,有时为了复用这些属性,会通过 style 从而可以复用这是属性,如下所示

<Button
    android:id="@+id/bt1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:style="@style/AccountActionButton"/>
    
<Button
    android:id="@+id/bt2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:style="@style/AccountActionButton"/>    
复制代码

属性

<style name="AccountActionButton" parent="@style/SettingTextFirstStyle">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:minHeight">40dp</item>
    <item name="android:gravity">center</item>
    <item name="android:textColor">@color/white</item>
    <item name="android:background">@drawable/bk_login_btn</item>
</style>
复制代码

通过以上方式,就可以方便的在一个地方去控制每个应用了 AccountActionButton style 的 View 属性,很方便。

但是一些情况下,我们创建 View 时是直接在代码中创建出来的 View,如下所示

Button btn = new Button(this)
复制代码

此时怎么应用 style 属性呢,如下所示

Button tvButton = new Button(new ContextThemeWrapper(getActivity(),R.style.AccountActionButton));
复制代码

OK !

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值