Android中style在布局文件中的应用

   android开发相对于ios有一个很大的优势,那就是可以通过xml来写布局,而且布局可以很灵活,能适应多种屏幕。但是时间久点你会发现xml中有太多的重复代码了,我真是恨死了在每次增加一个控件的时候都要不情愿的写上

1
2
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"

这两行, 如果我们有5个button控件都是16dip白色字体、背景为#0033FF、内容居中,那么我们必须在每个button上都加上这样几个属性:

1
2
3
4
android:textColor= "#FFFFFF"
android:textSize= "16dip"
android:background= "#0033FF"
android:gravity= "center"

   是不是很烦,是不是很sb!

   其实我们可以用将这些重复的属性的定义写在style文件中,如果某个控件需要16dip白色字体、背景为#0033FF的样式,只需将包含这些属性定义的style引用进来就可以了。

   先看看用style怎么设置这些重复的属性:

在values下新建style.xml

1
2
3
4
5
6
7
8
9
10
11
<?xml version= "1.0" encoding= "utf-8" ?>
<resources>
<style name= "white_blue_button" >
   <item name= "android:layout_width" >wrap_content</item>
   <item name= "android:layout_height" >wrap_content</item>
   <item name= "android:textSize" >16dip</item>
   <item name= "android:textColor" > #FFFFFF</item>
   <item name= "android:gravity" >center</item>
   <item name= "android:background" > #0033FF</item>
</style>
</resources>

   再回到我们的布局文件在按钮中设置style="@style/white_blue_button"就可以了。

1
2
3
4
<Button
     style= "@style/white_blue_button"
     android:text= "白色字体的蓝色按钮"
  />

   上面的style.xml文件中我们只定义了一组名为white_blue_button的样式给白色字体的蓝色按钮使用,其实在style中还可以定义多个样式给不同的组件使用。下面的style文件中我们定义了一组样式给button,还定义了一组样式给ImageView:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?xml version= "1.0" encoding= "utf-8" ?>
<resources>
<style name= "homebtn" >
   <item name= "android:layout_width" >wrap_content</item>
   <item name= "android:layout_height" >wrap_content</item>
   <item name= "android:textSize" >20sp</item>
   <item name= "android:textColor" > #FFFFffff</item>
   <item name= "android:gravity" >center</item>
   <item name= "android:layout_marginTop" >15dip</item>
   <item name= "android:layout_centerHorizontal" > true </item>
   <item name= "android:background" >@drawable/selector</item>
</style>
<style name= "homeiv" >
   <item name= "android:layout_width" >wrap_content</item>
   <item name= "android:layout_height" >wrap_content</item>
   <item name= "android:layout_marginTop" >15dip</item>
   <item name= "android:layout_centerHorizontal" > true </item>
</style>
</resources>

   这就是多个样式的例子,只需为不同的样式加上name就可以了。

   上面的例子中我们发现"homebtn"完全继承了"homeiv"中的属性,因此还可以进一步简化:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?xml version= "1.0" encoding= "utf-8" ?>
<resources>
<style name= "homeiv" >
   <item name= "android:layout_width" >wrap_content</item>
   <item name= "android:layout_height" >wrap_content</item>
   <item name= "android:layout_marginTop" >15dip</item>
   <item name= "android:layout_centerHorizontal" > true </item>
   <item name= "android:background" >@drawable/home</item>
</style>
<style name= "homebtn" parent= "@style/homeiv" >
   <item name= "android:textSize" >20sp</item>
   <item name= "android:textColor" > #FFFFffff</item>
   <item name= "android:gravity" >center</item>
   <item name= "android:background" >@drawable/selector</item>
</style>
</resources>

在homebtn中使用parent="@style/homeiv"来继承"homeiv"


   当然style最主要的作用不是用来精简代码,而是让开发者自定义更个性的效果,不过这个附加的作用也不错。拿button来说,一般我们会定义好几种风格的button,比如文章开头展示的那几种,最规范的办法是将这些风格的button在style中先定义好,然后应用到界面中去。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
是的,在Android系统,`style`用于定义控件的风格。 `style`是一种用于定义控件样式的资源类型,它可以包含一系列的属性值,如字体、颜色、背景等。我们可以通过定义一个`style`,来设置应用程序多个控件的共同属性,从而达到统一的风格效果。 例如,以下代码展示了如何定义一个`style`: ``` <style name="MyTextViewStyle"> <item name="android:textColor">#FFFFFF</item> <item name="android:textSize">18sp</item> <item name="android:background">#000000</item> </style> ``` 在代码,`<style>`标签用于定义一个名为`MyTextViewStyle`的样式,`<item>`标签用于定义该样式包含的属性。例如,`android:textColor`用于设置文字颜色,`android:textSize`用于设置文字大小,`android:background`用于设置背景颜色。 在应用程序的布局文件,我们可以通过设置`style`属性来应用该样式。例如,以下代码展示了如何使用上述定义的`style`来设置一个`TextView`控件的样式: ``` <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Hello, World!" style="@style/MyTextViewStyle" /> ``` 在代码,`style`属性被设置为`@style/MyTextViewStyle`,表示该控件的样式应该按照名为`MyTextViewStyle`的样式来设置。 需要注意的是,`style`属性可以同时包含多个样式,例如可以设置为`style="@style/Style1 @style/Style2"`,表示该控件的样式应该同时包含`Style1`和`Style2`的属性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值