Android 之主题

创建一个应用的主题

  为了定义主题,打开 res/values/styles.xml:

<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="MyAppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
<item name="android:typeface" >serif</item>
<item name="android:textColor">#000000</item>
<item name="android:textSize" >13pt</item>
<item name="android:shadowDx" >1.5</item>
<item name="android:shadowDy" >1.5</item>
<item name="android:shadowRadius" >1</item>
<item name="android:shadowColor" >#AAAAAA</item>
<item name="android:gravity" >center</item>
</style>

<style name="ButtonStyle">
<item name="android:layout_width" >300dp</item>
<item name="android:layout_height" >wrap_content</item>
<item name="android:layout_marginTop" >10dp</item>
<item name="android:textSize" >10pt</item>
<item name="android:background" >#FF0000</item>
</style>

</resources>

这里定义主题名是MyAppTheme 和<parent>根元素,这是从已有的主题中继承,也定义了样式名为ButtonStyle,包括一些属性,还定义了Button的主题,这里没有使用<parent>,因为是可选的。

下面是应用这个主题,这是通过 AndroidManifest.xml 完成,表示整个应用都是使用这个主题:

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

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

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/MyAppTheme" >
<activity
android:name="com.jdon.android.stylesthemestest.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配置中,指定了样式为我们之前定义的MyAppTheme,为了设置样式,还要加上活动的配置。

创建活动布局

  下面是创建一个活动的布局Layout,我们使用一个简单的布局,打开res/layout/activity_main.xml:

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

<TextView
android:id="@+id/customText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Text 1..." />

<TextView
android:id="@+id/bigText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/customText"
android:text="Text 2..." />

<Button
android:id="@+id/simpleTextButton"
android:layout_below="@id/bigText"
style="@style/ButtonStyle"
android:text="SimpleStyle for Text 1" />

<Button
android:id="@+id/bigTextButton"
android:layout_below="@id/simpleTextButton"
style="@style/ButtonStyle"
android:text="BigPurpleStyle for Text 2" />

</RelativeLayout>

在这个布局中,我们也是有了我们定义的样式ButtonStyle。

下面编制主活动src/com.jdon.android.stylesthemestest/MainActivity.java

public class MainActivity extends Activity {

private Button simpleBtn, bigBtn;
private TextView text1, text2;

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

text1 = (TextView) findViewById(R.id.customText);
text2 = (TextView) findViewById(R.id.bigText);

simpleBtn = (Button) findViewById(R.id.simpleTextButton);
simpleBtn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
text1.setTextAppearance(getApplicationContext(), R.style.SimpleStyle);
}
});

bigBtn = (Button) findViewById(R.id.bigTextButton);
bigBtn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
text2.setTextAppearance(getApplicationContext(), R.style.SimpleStyle_BigPurple);
}
});
}

}

[b]http://www.oschina.net/question/234345_40092[/b]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值