创建一个应用的主题

  为了定义主题,打开 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);
}
});
}

}

http://www.oschina.net/question/234345_40092

更多相关文章

  1. Android关于自定义ExpandableListView样式
  2. Android上自定义进度条的教学讲解【转】
  3. Android中使用自定义Dialog时match_parent无效的问题
  4. Android自定义Dialog对话框
  5. Android主题与Toolbar样式之间的关系
  6. Android的7种进度条样式
  7. Android应用开发中的风格和主题(style,themes)
  8. Android 创建自定义View 实现TopBar
  9. Android Drawable系列(1):自定义背景以及注意事项

随机推荐

  1. 如何在HTML标记上打印/显示动态值?
  2. css+html实现自适应宽度的菜单学习
  3. 用HTML做的简单的个人简历
  4. 具有不间断空格的呈现字符串
  5. 经常使用的20个css高级技巧(下)
  6. 创建mailto超链接,该超链接将在Outlook中
  7. 如何根据容器更改元素的宽度?
  8. 使用jsPDF生成一个保存HTML页面样式的pdf
  9. 如何定义两个日期之间的Kendo网格列过滤
  10. How to prevent Twitter Bootstrap chang