Android 用style简化layout布局文件

本文介绍如何通过定义样式(style)来减少Android XML布局文件中的重复代码,并展示了如何为Button和ImageView设置统一的样式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我有一个页面上面有若干个button样式都是相同的 在屏幕居中 25号白色字

之前的代码如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="@color/background">
 <ImageView
 	android:id="@+id/home_iv1"
  	android:layout_width="wrap_content"
  	android:layout_height="wrap_content"
  	android:paddingTop="15dip"
  	android:layout_centerHorizontal="true"
  	android:src="@drawable/home"
  	/>
  	<Button
  		android:id="@+id/home_bt1"
  		android:layout_width="wrap_content"
  		android:layout_height="wrap_content"
  		android:layout_centerHorizontal="true"
  		android:layout_below="@id/home_iv1"
  		android:layout_marginTop="15dip"
  		android:background="@drawable/selector"
  		android:gravity="center"
  		android:text="@string/home_b1"
  		/>
  	........
  	<Button
  	  	android:id="@+id/home_bt5"
  		android:layout_width="wrap_content"
  		android:layout_height="wrap_content"
  		android:layout_centerHorizontal="true"
  		android:layout_below="@id/home_bt4"
  		android:layout_marginTop="15dip"
  		android:background="@drawable/selector"
  		android:gravity="center"
  		android:text="@string/home_b5"
  		/>
</RelativeLayout>

 

我们可以看到这五个按钮除了ID和text、还有below(因为用的相对布局,如果用线性布局这块也可以相同)其他的都一样,那我们能不能简化一下这些重复的代码呢?可以,用style。

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

在values下新建style.xml

<?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>
</resources>

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

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="@color/background">
 <ImageView
 	android:id="@+id/home_iv1"
  	android:layout_width="wrap_content"
  	android:layout_height="wrap_content"
  	android:paddingTop="15dip"
  	android:layout_centerHorizontal="true"
  	android:src="@drawable/home"
  	/>
  	<Button
  		android:id="@+id/home_bt1"
  		style="@style/homebtn"
  		android:layout_below="@id/home_iv1"
  		android:text="@string/home_b1"
  		/>
  	.......
    	<Button
  	  	android:id="@+id/home_bt5"
  		style="@style/homebtn"
  		android:layout_below="@id/home_bt4"
  		android:text="@string/home_b5"
  		/>
</RelativeLayout>

 

其实在style中可以定义多个样式给不同的组件使用,现在我们把ImageView也用样式定义一下吧:

<?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>

 

 好了介绍到这里也就完了。等。。等一下这里好像出现了和刚才的布局文件一样的问题,重复的代码!(我是不是有重复癖,看见重复就想消灭。。)

这里的重复能不能消灭呢,这也是可以的。在style下还有一个个有用的属性:

parent – 可选,一些在自定义的style中没有指定的属性会继承parent style中的值。parent可以是android预定义的resource,也可以是自己定义的style。
现在看看使用parent之后的样子:

<?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>

 

当然style最主要的作用不是用来精简代码,而是让开发者自定义更个性的效果,不过这个附加的作用也不错。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值