Android笔记 theme主题

本文详细介绍了Android开发中theme与style的区别及应用方法,包括如何定义theme和style、如何在Activity中应用不同的theme,以及theme的继承等高级用法。

Android中theme与style的定义没有区别

区别在于控制范围 theme范围更大

style用于控制view对象

theme用于控制Activity样式

1在res/values下定义theme: 

<style name="red_theme">
        <item name="android:background">#ff0000</item>
</style>

(没错 就是style节点)

2Activity应用主题

在相应的Activity节点加如下属性

android:theme="@style/red_theme"

(注:如果将以上属性添加到application节点下 则当前application所有Activity都应用该主题 如在Activity再配置theme 会覆盖application中配置的)


另:几个属性

windowNoTitle 无标题 

windowFullScreen全屏

3theme的继承

<style name="aa_theme" parent="@android:style/Theme.Light.NoTitleBar">
        <item name="android:windowBackground">@drawable/aa</item>
</style>

会发现标题栏不见了

实例

a布局

<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=".MainActivity" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="click2"
        android:text="开启二" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="click3"
        android:text="开启三" />

</LinearLayout>

b三个Activity

package com.example.a108theme;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

	public void click2(View view) {
		Intent intent = new Intent(this, Activity02.class);
		startActivity(intent);
	}

	public void click3(View view) {
		Intent intent = new Intent(this, Activity03.class);
		startActivity(intent);
	}

}

package com.example.a108theme;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

public class Activity02 extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		TextView tv = new TextView(this);
		tv.setText("我是界面02");
		setContentView(tv);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

package com.example.a108theme;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

public class Activity03 extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		TextView tv = new TextView(this);
		tv.setText("我是界面03");
		setContentView(tv);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
c style样式

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

    <!--
        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>

<strong>    <style name="red_theme">
        <item name="android:background">#ff0000</item>
    </style>

    <style name="green_theme">
        <item name="android:background">#00ff00</item>
    </style>

    <style name="aa_theme" parent="@android:style/Theme.Light.NoTitleBar">
        <item name="android:windowBackground">@drawable/aa</item>
    </style></strong>

</resources>

d配置Activity

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/red_theme">
        <activity
            android:name="com.example.a108theme.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>
        <strong><activity android:name="com.example.a108theme.Activity02" android:theme="@style/green_theme" >
        </activity>
        <activity android:name="com.example.a108theme.Activity03" android:theme="@style/aa_theme">
        </activity></strong>
    </application>

</manifest>








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值