Android中的Style是一些属性的集合,包括height,padding,font color,background等等,基本上布局文件中的属性,这里都可以找到。Style单独定义在xml文件中,类似与web页面中css的角色,将设计和内容分开,便于修改和重复使用,一方面可以提高代码的复用性,另一方面也能保证UI视图的一致性,同时可以增强代码的可维护性。
下面通过一个实例学习一下Style的用法,在values文件夹下的styles文件下,添加如下代码:
<style name="textStyle">
<item name="android:textSize">18sp</item>
<item name="android:textColor">#00ffff</item>
<item name="android:gravity">center_horizontal</item>
<item name="android:background">#ffffff</item>
<item name="android:layout_margin">5dp</item>
</style>
可以看出一个样式表由Style标签包裹,同时定义了name属性用于引用。标签中每一个Item就是一个属性,这里设置了字体大小、字体颜色、布局位置、背景、margin等属性。
在TextView中用style引入我们定义的样式表,如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/textStyle"
android:text="Hello World!" />
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/textStyle"
android:text="Hello World!" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/textStyle"
android:text="Hello World!" />
</LinearLayout>
这时运行实例如下:
可以看出每一个TextView都是同样的样式。
当然,设置了style之后还是可以继续设置style里的属性,同时也可以在代码中设置相应的属性,修改代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/textStyle"
android:textSize="24dp"
android:text="Hello World!" />
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/textStyle"
android:text="Hello World!" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/textStyle"
android:text="Hello World!" />
</LinearLayout>
这时效果如下:
在代码里设置:
public class MainActivity extends AppCompatActivity {
private TextView mTexView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTexView=(TextView)findViewById(R.id.tv);
mTexView.setTextSize(14);
}
}
运行实例如下:
可以看出,同时在Style、布局文件中和代码中同时设置同一属性时,代码中设置优先级最高、其次是布局文件、优先级最低的是在Style中设置。
为了更好地体现代码的复用性,Style也支持继承,查看如下代码:
<style name="red">
<item name="android:textColor">@color/red</item>
</style>
<style name="textStyle" parent="red">
<item name="android:textSize">18sp</item>
<item name="android:gravity">center_horizontal</item>
<item name="android:background">#ffffff</item>
<item name="android:layout_margin">5dp</item>
</style>
可以通过parent属性获得Style的继承,运行实例如下:
可以看出,textStyle继承了red的textColor属性。除了单继承之外,还支持多重继承,如下:
<style name="red">
<item name="android:textColor">@color/red</item>
</style>
<style name="textStyle" parent="red">
<item name="android:gravity">center_horizontal</item>
<item name="android:layout_margin">5dp</item>
</style>
<style name="textStyle2" parent="textStyle">
<item name="android:background">#fff00f</item>
</style>
运行实例如下:
除了使用parent继承之外,对于自定义的Style还可以使用"."进行继承,如下:
<style name="red">
<item name="android:textColor">@color/red</item>
</style>
<style name="textStyle" parent="red">
<item name="android:gravity">center_horizontal</item>
<item name="android:layout_margin">5dp</item>
</style>
<style name="textStyle.textStyle">
<item name="android:background">#fff00f</item>
</style>
运行实例可以得到一样的效果。
除了可以继承自定义的Style之外,还可以继承系统Style:
<style name="textStyle" parent="Base.TextAppearance.AppCompat.Display4">
<item name="android:gravity">center_horizontal</item>
<item name="android:background">#ffffff</item>
<item name="android:layout_margin">5dp</item>
运行实例:
谢谢关注我的微信公众号,觉得好可以分享到朋友圈哦.
请关注我的新浪微博:AndroidTip
CSDN博客:http://blog.csdn.net/yayun0516