项目过程中,在layout文件中有大量的控件属性有重复,比如width,height等,为了简化代码,于是将这些重复的属性统一写进了styles.xml文件中,自定义了包含这些属性的style。
使用方法及示例总结如下:
在res/values/styles中类似这样写就可以 :
<style name="button_style">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_marginTop">5dp</item>
<item name="android:layout_marginLeft">20dp</item>
<item name="android:layout_marginRight">20dp</item>
<item name="android:background">@drawable/button_click</item>
</style>
在layout中调用:
style="@style/button_style"
注:已经在 style中写的东西也可以在button 下出现。对style中的属性值进行覆盖
在layout中完整的使用示例:
<Button
android:id="@+id/addLessonBtn"
style="@style/button_style"
android:text="@string/add_course_add" />