android样式和主题

样式和主题概述 

共同点:(1)定义的方式是一样的

不同点:(1)style作用范围比较窄(控件 button textview),(2)theme作用在activity或者Application节点下范围比较宽

一、项目目录结构

二、activity_main.xml界面

三、activity_main.xml代码

<LinearLayout 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"
    android:orientation="vertical"
    tools:context="com.zgs.StyleAndTheme.MainActivity" >

    <!-- 以下设置了自己定义的样式 -->
    <TextView
        style="@style/my_style"
        android:text="aaa" />

    <TextView
        style="@style/my_style"
        android:text="bbb" />

    <TextView
        style="@style/my_style"
        android:text="ccc" />

    <TextView
        style="@style/my_style"
        android:text="ddd" />

</LinearLayout>
四、styles.xml代码
<resources>

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.

    -->
    <style name="AppBaseTheme" parent="android:Theme.Light">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.

        -->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

    <style name="my_style">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:textColor">#ff0000</item>
        <item name="android:textSize">45sp</item>
    </style>
    
    <style name="my_theme">
        <item name="android:background">#ADFF2F</item>
    </style>

</resources>
特殊说明:样式和主题不一定非得定义在styles.xml文件中,可任意新建个xml文件,然后在里面定义样式和主题。

五、AndroidManifest.xml代码

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

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="22" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/my_theme" > <!-- 此处设置了自己定义的主题 -->
        <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>
六、效果演示

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值