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 !