什么是样式?
答:
一个包含一个或者多个view控件属性的集合。
如何引用自定义的样式?
答:
自定义样式:
<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="Theme.AppCompat.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="pengTheme">
<item name="android:windowNoTitle">true</item>
</style>
<!-- 自定义的样式-->
<style name="pengStyleText">
<item name="android:textSize">18sp</item>
<item name="android:background">#ffff0000</item>
<item name="android:gravity">center</item>
</style>
<style name="pengStyleText.pengStyleText1">
</style>
</resources>
在布局中使用样式:
<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="net.peng.app.androidui.MainActivity$PlaceholderFragment" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/pengStyleText.pengStyleText1"
android:text="@string/hello_world" />
</RelativeLayout>
怎样实现样式之间的继承?
答:两种方式
1、<style name="pengStyleText" parent=“pengStyle”>
2、<style name="pengStyle.pengStyleText" >
1和2都实现了"pengStyleText继承pengStyle,但是第二种引用时要写成pengStyle.pengStyleText。
注:父样式的值在子样式中可以修改。
android开发中主题是什么?
答:类似样式,但是应用于给定Activity的所有元素,即整个屏幕(可能不太准确)。
如何自定义的主题以及引用自定义的主题?
答:
自定义主题,见自定义样式中的代码;
引用自定义的主题:
两种方式:
1、在java代码中引用
demo:
package net.peng.app.androidui;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(R.style.pengTheme);//引用自定义的主题
setContentView(R.layout.main);
}
}
2、在AndroidManifest.xml中使用
demo:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.peng.app.androidui"
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" >
<!-- 放到这里,所有的activity都将使用pengTheme -->
<!-- android:theme="@style/pengTheme" -->
<activity
android:name="net.peng.app.androidui.MainActivity"
android:label="@string/app_name" > <!-- 放到这里,只有net.peng.app.androidui.MainActivity使用pengTheme -->
android:theme="@style/pengTheme" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>