android-样式和主题(style&theme)

本文介绍了Android中如何定义和使用样式来控制界面元素的显示风格,包括基本样式定义、样式继承及属性重写等关键概念。

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

android中的样式和CSS样式作用相似,都是用于为界面元素定义显示风格,它是一个包含一个或者多个view控件属性的集合。如:需要定义字体的颜色和大小。

Android中可以这样定义样式:

在res/values/styles.xml文件中添加以下内容

<?xml version="1.0"encoding="utf-8"?>
<resources>
    <style name=“wxxu”><!-- 为样式定义一个全局唯一的名字-->
        <item name=“android:textSize”>18px</item> <!-- name属性的值为使用了该样式的View控件的属性 -->
        <item name="android:textColor">#0000CC</item>
    </style>
</resources>

在layout文件中可以像下面这样使用上面的android样式:

<?xml version="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"....>
    <TextView style="@style/wxxu"
        ..... />
</LinearLayout>

style可以继承,用parent熟悉来继承

<?xml version="1.0"encoding="utf-8"?>
<resources>
    <style name="wxxu">
        <item name="android:textSize">18px</item> <!-- name属性为样式要用在的View控件持有的属性 -->
        <item name="android:textColor">#0000CC</item>
    </style>
    <style name="subwxxu" parent="@style/wxxu">
        <item name="android:textColor">#FF0000</item>
    </style>
</resources>
继承后可以重写父样式的属性,如下:

<?xml version="1.0"encoding="utf-8"?>
<resources>
    <style name="wxxu">
        <item name="android:textSize">18px</item> <!-- name属性为样式要用在的View控件持有的属性 -->
        <item name="android:textColor">#0000CC</item>
    </style>
    <style name="subwxxu" parent="@style/wxxu">
        <item name="android:textColor">#FF0000</item>
    </style>
</resources>

第二中继承方式,用点(.)来实现继承,如下继承childStyle养生

  <style name="childStyle" parent="textViewStyle">
  	<item name="android:layout_width">fill_parent</item>
  	<item name="android:layout_height">wrap_content</item>
  	<item name="android:textColor">#00FF00</item>
  </style>
  
  <style name="childStyle.liming">
  	<item name="android:textColor">#0000FF</item>
  </style>
适用

<TextView  
    style="@style/childStyle.liming"
    android:text="@string/hello"
    />

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值