Android樣式化的定性物件--Style樣式的定義
主程式:
package cn.com.hyron.activity;
import android.app.Activity;
import android.os.Bundle;
public class StyleActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
非常乾淨,只有載入R.layout.main定義佈局內容而已,但由於定義在main.xml裡的語句不同,自然也有不同的樣貌呈現。
res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/white"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<!-- 套用樣式1的TextView -->
<TextView
style="@style/DavidStyleText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:text="@string/str_text_view1"
/>
<!-- 套用樣式2的TextView -->
<TextView
style="@style/DavidStyleText2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:text="@string/str_text_view2"
/>
</LinearLayout>
res/values/style.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="DavidStyleText1">
<item name="android:textSize">18sp</item>
<item name="android:textColor">#EC9237</item>
</style>
<style name="DavidStyleText2">
<item name="android:textSize">14sp</item>
<item name="android:textColor">#FF7F7C</item>
<item name="android:fromAlpha">0.0</item>
<item name="android:toAlpha">0.0</item>
</style>
</resources>