android使用theme和style


theme和style其实使用方式是一样的,不同的是定义他们的时候,里面的item内容是不一样的,theme的item一般都含有window之类的,而style一般都是View的外观设置,比如textcolor,layout_height之类的,theme用在Activity里面设置样式而style用在View上面设置view的外观。要使用他们,只需要在res/values目录下面新建一个styles.xml文件,在里面定义style,然后在使用的地方使用@style/XXX就可以了。先来看看这个文件是什么样的:

<resources>

    <style name="AppBaseTheme" parent="Theme.AppCompat.Light"></style>

    <style name="AppTheme" parent="AppBaseTheme"></style>

</resources>

这个里面定义两个style,分别为APPBaseTheme和AppTheme,APPBaseTheme的父类为Them.AppCompat.Light,APPTheme的父类为APPTheme,所以AppTheme拥有所有APPBaseTheme定义的item。表面上看起来里面什么都没定义,应该没有什么效果,我们先来找到Theme.AppCompat.Light看这里定义了些什么:


我们发现他的父类是Base.Theme.AppCompat.Light,我们看看这个parent里面定义了什么:



我们发现他还有个parent,下面看看这个parent里面是什么:


这个只是我截取的一部分,这里定义的所有的属性我们的APPBaseTheme都可以访问到,这些都是系统给我们定义的,如果我们引用了AppBaseTheme,而我们自己又定义了和他相同的item,那么最后的显示效果还是我们自己定义的,子类的item会覆盖掉父类的,view自己的item会覆盖掉style里面的item。

ok,下面我们自己来定义一个style然后运用到TextView上面。首先我们新建一个工程,不修改里面的代码,只修改布局文件。先来看看我们没修改时的布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        style="@style/AlertDialog.AppCompat"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_centerInParent="true"
        android:background="#ccc"
        android:rotation="15"
        android:text="@string/hello_world"
        android:textColor="#c0c"
        android:textSize="13sp" />

</RelativeLayout>

里面只有一个TextView,下面来看看运行效果:


然后我们在styles.xml文件中定义一个style,textview上面引用style达到和上面显示效果一致,这个style的名字叫做TextViewStyle,里面的代码如下:

<resources xmlns:android="http://schemas.android.com/apk/res/android">

    <style name="AppBaseTheme" parent="Theme.AppCompat.Light"></style>
    <style name="AppTheme" parent="AppBaseTheme"></style>

    <style name="TextViewStyle">
        <item name="android:textSize">13sp</item>
        <item name="android:textColor">#c0c</item>
        <item name="android:rotation">15</item>
        <item name="android:width">200dp</item>
        <item name="android:height">200dp</item>
        <item name="android:background">#ccc</item>
    </style>


</resources>
下面来看看布局文件的改动:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        style="@style/TextViewStyle"
      android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_centerInParent="true"
        android:text="@string/hello_world" />

</RelativeLayout>

这样改动之后,我们运行看看效果是什么样的:



我们看到是一样的,这就是自定义style的使用。


下面来看看theme的使用,其实这个项目中我们是有使用到上面定义的style里面的AppTheme的,我们来看看项目的manifest文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.xinxue.themedemo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

我们看到application标签里有一个android:theme选项里面就引用到了我们先前定义的AppTheme,下面我们修改AppTheme,把引用的背景改成绿色,修改后的style文件代码为:

<resources xmlns:android="http://schemas.android.com/apk/res/android">

    <style name="AppBaseTheme" parent="Theme.AppCompat.Light"></style>

    <style name="AppTheme" parent="AppBaseTheme">
        <item name="android:background">#0f0</item>
    </style>

    <style name="TextViewStyle">
        <item name="android:textSize">13sp</item>
        <item name="android:textColor">#c0c</item>
        <item name="android:rotation">15</item>
        <item name="android:width">200dp</item>
        <item name="android:height">200dp</item>
        <item name="android:background">#ccc</item>
    </style>

</resources>

就添加了一个item,下面来看看其运行效果:


扫描关注我的微信公众号:



可见我们定义的item起作用了,ok,theme的使用是不是和style是一样的呢???以后我们的应用想改变样式,只需要来到这个styles.xml文件中做修改就可以啦~~~~~~~···

本来想着这节很简单就不给demo了,考虑到一些特殊情况,还是提供吧:点击打开链接


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值