18 UI美化自定义主题样式代码

来源:互联网 发布:橱柜哪个牌子好 知乎 编辑:程序博客网 时间:2024/05/22 08:24
  • 自定义主题 假设我们我们对现有的样式不大满意 那么可在工程目录res/values下的styles.xml自定义

方法:
1. res/values下的styles.xml文件中自定义一个标签

<style name ="你想要取的样式名" parent="你想继承的样式"> <item name="重写的属性名">新属性</item>  <item name="...">...</item> . .</style>

例子: styles.xml文件增加的字段

 <color name="customcolor">#b0b0ff</color>    <!-- 自定义的主题    name ="主题的名称 " parent="继承于  系统主题"   改变背景颜色 -->    <!-- parent="@style/android:Theme.Light" 也可以写成 parent="android:Theme.Light" -->    <style name="CustomTheme" parent="@style/android:Theme.Light">        <!--  这里必须自定义一个color放入 不然报错 -->        <item name="android:windowBackground">@color/customcolor</item>    </style>

使用:在清单文件中AndroidManifest.xml中apliction或者activity中使用(如果activity没有定义主题那么默认使用apliction的)

例子:

 <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name="com.qf.day18_resource_demo.MainActivity"            android:label="@string/app_name"             android:theme="@style/CustomTheme"            >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application>
  • 关于继承样式两种方式

    • 方式一
    <style name="father">         <item name="android:textColor">#f00</item>          <item name="android:layout_width">wrap_content</item>    </style>    <!-- 继承父类所有属性 并且可重写父类属性 -->     <style name="father.son">          <item name="android:layout_width">wrap_content</item>    </style>

    调用方式

     <activity            android:name="com.qf.day18_resource_demo.MainActivity"            android:label="@string/app_name"             android:theme="@style/father.son"            >
    • 方式二
      <style name="father">         <item name="android:textColor">#f00</item>          <item name="android:layout_width">wrap_content</item>    </style>    <!-- 继承父类所有属性 并且可重写父类属性 -->     <style name="son" parent="@style/father">          <item name="android:layout_width">wrap_content</item>    </style>
    • 调用
 <activity            android:name="com.qf.day18_resource_demo.MainActivity"            android:label="@string/app_name"             android:theme="@style/son"            >

  • 假设我们有一天很多控件的样式都是一样的 我们可以抽取样式放入style中
 <Button         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textColor="@android:color/holo_red_light"        android:text="抽奖"        />    <Button         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textColor="@android:color/holo_red_light"        android:text="再来一次"        />    <Button         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textColor="@android:color/holo_red_light"        android:text="不玩了"        />
  • 上面的例子可以看到三个Button有多个重复样式 ,那么我们现在抽取公共样式 于
    目录res/values下的styles.xml自定义
<style name="MyButtonStyle">        <item name="android:layout_width">wrap_content</item>        <item name="android:layout_height">wrap_content</item>        <item name=" android:textColor">@android:color/holo_red_light</item>    </style>

使用方法:

 <Button         style="@style/MyButtonStyle"        android:text="抽奖"        />    <Button        style="@style/MyButtonStyle"        android:text="再来一次"        />    <Button        style="@style/MyButtonStyle"        android:text="不玩了"
0 0
原创粉丝点击