一个样式相当于多个属性设置的集合,其他 UI 组件通过 style 属性来指定样式,这就相当于把该样式包含的所有属性设置同时应用于该 UI 组件。
Android 的样式资源文件放在 /res/values 目录下,样式资源文件的根元素是 ,该元素可以包含多个
name:指定样式的名称。
parent: 指定样式所继承的父样式。
下面是一个简单的使用示例,首先是自定义的 Style 内容,将它加在 /res/values 目录下的 styles.xml 文件中:
@color/colorPrimary
@color/colorPrimaryDark
@color/colorAccent
20sp
@color/colorPrimaryDark
#EEEE66
8dp
#000000
主布局文件的内容如下:
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorGray"
android:orientation="vertical"
android:id="@+id/container"
>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="@string/testStyle01"
style="@style/test_style01"
/>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="@string/testStyle02"
style="@style/test_style02"
/>
主程序文件的代码:
package com.toby.personal.testlistview;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
final private static String TAG = "Toby_Test";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
程序的运行效果:
运行效果
参考文献:《疯狂Android讲义(第2版)》