Android中的自定义主题和样式

本文介绍如何使用XML文件来自定义Android应用的主题和样式。通过创建style文件并定义不同的样式属性,可以轻松改变应用中TextView等组件的外观。文章提供了具体的XML代码示例,包括设置背景颜色和其他布局属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

效果如下图:


通过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>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值