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>