style和theme

1. style

先看代码,
布局文件:

<TextView
        android:text="text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="18sp"
        android:background="#a0a0a0"
        android:padding="20dp"
        android:layout_margin="20dp"
        android:textColor="#66ccff"/>
    <TextView
        android:text="text2"
        style="@style/MyStyle"/>
    <ImageView
        android:src="@mipmap/ic_launcher"
        style="@style/MyStyle"/>

style文件:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
    <style name="MyStyle">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:textSize">18sp</item>
        <item name="android:padding">20dp</item>
        <item name="android:layout_margin">20dp</item>
        <item name="android:gravity">center</item>
        <item name="android:textColor">#66ccff</item>
        <item name="android:background">#a0a0a0</item>

    </style>

</resources>

显示效果:

image

可以发现,style就是把控件中相同的属性抽出来,统一引用,在同一个app中便于统一显示效果和修改。

2. 继承

style有两种继承方式:

1. 通过“.”继承,在符style后加“.”进行继承,这种方式适用于自定义的style,不能用于继承系统style。
<style name="CodeFont.Red.Big">
        <item name="android:textSize">30sp</item>
    </style>
2. 通过“parent”继承,这种方式用于所有已有style,如果使用这种继承的方式的话,命名时的“.”继承方式就会失效,只作为一种命名的方式。
<style name="GreenText" parent="@android:style/TextAppearance">
        <item name="android:textColor">#00FF00</item>
    </style>

3. theme

我们注意到theme都是在style中定义的,这说明,他只是一种特殊的style,其用法和style是相同的。
theme和其他style不同的地方主要在于其中的属性不同,以及应用的地方不同。

  • theme是应用在application和activity中的,所以其中的属性主要都是定义窗体的,如修改状态栏,应用标题等,通常在androidmanifeat.xml中使用;
  • 普通style是用于定义view或viewgroup的所以其主要定义的是控件的属性。

theme中的常见属性:
image

所有属性见R.attr

所有可用的style和theme见R.style

google api指南

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值