效果如下图:
通过xml文件来设置主题和样式:
style文件自定义样式和主题的代码:
<?xml version="1.0" encoding="utf-8"?>
<resources>
//设置父主题,取自TextAppearance
<style name="tmacskyTheme" parent="@android:style/TextAppearance">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_weight">1</item>
</style>
//设置子主题
<style name="tmacskyTheme.textRed">
<item name="android:background">@color/red</item>
</style>
<style name="tmacskyTheme.textBlue">
<item name="android:background">@color/blue</item>
</style>
<style name="tmacskyTheme.textGreen">
<item name="android:background">@color/green</item>
</style>
//设置颜色
<color name="red">#FF0000</color>
<color name="green">#00FF00</color>
<color name="blue">#0000FF</color>
</resources>
xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
style="@style/tmacskyTheme.textBlue"
android:text="TextView1" />
<TextView
android:id="@+id/textView2"
style="@style/tmacskyTheme.textGreen"
android:text="TextView2" />
<TextView
style="@style/tmacskyTheme.textRed"
android:id="@+id/textView3"
android:text="TextView3" />
</LinearLayout>
本文介绍如何使用XML文件来自定义Android应用的主题和样式。通过创建style文件并定义不同的样式属性,可以轻松改变应用中TextView等组件的外观。文章提供了具体的XML代码示例,包括设置背景颜色和其他布局属性。
181

被折叠的 条评论
为什么被折叠?



