---
title: "Android 用style简化layout布局文件"
author: "cloklo"
date: "2013-08-23T23:56:44Z"
tags: []
categories: ["编程语言"]
slug: "android-style-layout"
draft: false
lastmod: "2013-08-23T23:56:44Z"
id: 1930259
url: "https://cloklo.iteye.com/blog/1930259"
---

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

　　之前的代码如下：

　　Xml代码 收藏代码

　　1. &lt; ?xml version="1.0" encoding="utf-8"?&gt;

　　2. &lt; RelativeLayout

　　3. xmlns:[Android](http://dev.10086.cn/cmdn/wiki/index.php?doc-view-1789)="[http://schemas.android.com/apk/res/android](http://schemas.android.com/apk/res/android)"

　　4. android:layout\_width="fill\_parent"

　　5. android:layout\_height="fill\_parent"

　　6. android:background="@color/background"&gt;

　　7. &lt; ImageView

　　8. android:id="@+id/home\_iv1"

　　9. android:layout\_width="wrap\_content"

　　10. android:layout\_height="wrap\_content"

　　11. android:paddingTop="15dip"

　　12. android:layout\_centerHorizontal="true"

　　13. android:src="@drawable/home"

　　14. /&gt;

　　15. &lt; Button

　　16. android:id="@+id/home\_bt1"

　　17. android:layout\_width="wrap\_content"

　　18. android:layout\_height="wrap\_content"

　　19. android:layout\_centerHorizontal="true"

　　20. android:layout\_below="@id/home\_iv1"

　　21. android:layout\_marginTop="15dip"

　　22. android:background="@drawable/selector"

　　23. android:gravity="center"

　　24. android:text="@string/home\_b1"

　　25. /&gt;

　　26. ........

　　27. &lt; Button

　　28. android:id="@+id/home\_bt5"

　　29. android:layout\_width="wrap\_content"

　　30. android:layout\_height="wrap\_content"

　　31. android:layout\_centerHorizontal="true"

　　32. android:layout\_below="@id/home\_bt4"

　　33. android:layout\_marginTop="15dip"

　　34. android:background="@drawable/selector"

　　35. android:gravity="center"

　　36. android:text="@string/home\_b5"

　　37. /&gt;

　　38. &lt; /RelativeLayout&gt;

　　&lt; ?xml version="1.0" encoding="utf-8"?&gt;

　　&lt; RelativeLayout

　　xmlns:android="[http://schemas.android.com/apk/res/android](http://schemas.android.com/apk/res/android)"

　android:layout\_width="fill\_parent"

　　android:layout\_height="fill\_parent"

　　android:background="@color/background"&gt;

　　&lt; 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"

　　/&gt;

　　&lt; 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"

　　/&gt;

　　........

　　&lt; 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"

　　/&gt;

　　&lt; /RelativeLayout&gt;

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

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

　　在values下新建style.xml

　　Xml代码 收藏代码

　　 &lt; ?xml version="1.0" encoding="utf-8"?&gt;

　　 &lt; resources&gt;

　　 &lt; style name="homebtn" &gt;

　　 &lt; item name="android:layout\_width"&gt;wrap\_content&lt; /item&gt;

　　 &lt; item name="android:layout\_height"&gt;wrap\_content&lt; /item&gt;

     &lt; item name="android:textSize"&gt;20sp&lt; /item&gt;

　　 &lt; item name="android:textColor"&gt;#FFFFffff&lt; /item&gt;

　　 &lt; item name="android:gravity"&gt;center&lt; /item&gt;

　　 &lt; item name="android:layout\_marginTop"&gt;15dip&lt; /item&gt;

　　 &lt; item name="android:layout\_centerHorizontal"&gt;true&lt; /item&gt;

　　 &lt; item name="android:background"&gt;@drawable/selector&lt; /item&gt;

　　 &lt; /style&gt;

　　 &lt; /resources&gt;

　　&lt; ?xml version="1.0" encoding="utf-8"?&gt;

　　&lt; resources&gt;

　　&lt; style name="homebtn" &gt;

　　&lt; item name="android:layout\_width"&gt;wrap\_content&lt; /item&gt;

　　&lt; item name="android:layout\_height"&gt;wrap\_content&lt; /item&gt;

　　&lt; item name="android:textSize"&gt;20sp&lt; /item&gt;

　　&lt; item name="android:textColor"&gt;#FFFFffff&lt; /item&gt;

　　&lt; item name="android:gravity"&gt;center&lt; /item&gt;

　　&lt; item name="android:layout\_marginTop"&gt;15dip&lt; /item&gt;

　　&lt; item name="android:layout\_centerHorizontal"&gt;true&lt; /item&gt;

　　&lt; item name="android:background"&gt;@drawable/selector&lt; /item&gt;

　　&lt; /style&gt;

　　&lt; /resources&gt;

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

　　Xml代码 收藏代码

　　 &lt; ?xml version="1.0" encoding="utf-8"?&gt;

　　 &lt; RelativeLayout

　　 xmlns:android="[http://schemas.android.com/apk/res/android](http://schemas.android.com/apk/res/android)"

　　 android:layout\_width="fill\_parent"

　　 android:layout\_height="fill\_parent"

　　 android:background="@color/background"&gt;

　　 &lt; 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"

　　 /&gt;

　　 &lt; Button

　　 android:id="@+id/home\_bt1"

　　 style="@style/homebtn"

　　 android:layout\_below="@id/home\_iv1"

　　 android:text="@string/home\_b1"

　　 /&gt;

　　 .......

　　 &lt; Button

　　 android:id="@+id/home\_bt5"

　　 style="@style/homebtn"

　　 android:layout\_below="@id/home\_bt4"

　　android:text="@string/home\_b5"

　　 /&gt;

　　&lt; /RelativeLayout&gt;

　　&lt; ?xml version="1.0" encoding="utf-8"?&gt;

　　&lt; RelativeLayout

　　xmlns:android="[http://schemas.android.com/apk/res/android](http://schemas.android.com/apk/res/android)"

　　android:layout\_width="fill\_parent"

　　android:layout\_height="fill\_parent"

　　android:background="@color/background"&gt;

　　&lt; 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"

　　/&gt;

　　&lt; Button

　　android:id="@+id/home\_bt1"

　　style="@style/homebtn"

　　android:layout\_below="@id/home\_iv1"

　　android:text="@string/home\_b1"

　　/&gt;

　　.......

　　&lt; Button

　　android:id="@+id/home\_bt5"

　　style="@style/homebtn"

　　android:layout\_below="@id/home\_bt4"

　　android:text="@string/home\_b5"

　　/&gt;

　　&lt; /RelativeLayout&gt;

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

　　Xml代码 收藏代码

　　&lt; ?xml version="1.0" encoding="utf-8"?&gt;

　　&lt; resources&gt;

　　&lt; style name="homebtn" &gt;

　 &lt; item name="android:layout\_width"&gt;wrap\_content&lt; /item&gt;

　　&lt; item name="android:layout\_height"&gt;wrap\_content&lt; /item&gt;

　　&lt; item name="android:textSize"&gt;20sp&lt; /item&gt;

　　&lt; item name="android:textColor"&gt;#FFFFffff&lt; /item&gt;

　　&lt; item name="android:gravity"&gt;center&lt; /item&gt;

　 &lt; item name="android:layout\_marginTop"&gt;15dip&lt; /item&gt;

　 &lt; item name="android:layout\_centerHorizontal"&gt;true&lt; /item&gt;

　　&lt; item name="android:background"&gt;@drawable/selector&lt; /item&gt;

　　&lt; /style&gt;

　　&lt; style name="homeiv"&gt;

　　&lt; item name="android:layout\_width"&gt;wrap\_content&lt; /item&gt;

　　&lt; item name="android:layout\_height"&gt;wrap\_content&lt; /item&gt;

　　&lt; item name="android:layout\_marginTop"&gt;15dip&lt; /item&gt;

　　&lt; item name="android:layout\_centerHorizontal"&gt;true&lt; /item&gt;

　　&lt; /style&gt;

　　&lt; /resources&gt;

　　&lt; ?xml version="1.0" encoding="utf-8"?&gt;

　　&lt; resources&gt;

　　&lt; style name="homebtn" &gt;

　　&lt; item name="android:layout\_width"&gt;wrap\_content&lt; /item&gt;

　　&lt; item name="android:layout\_height"&gt;wrap\_content&lt; /item&gt;

　　&lt; item name="android:textSize"&gt;20sp&lt; /item&gt;

　　&lt; item name="android:textColor"&gt;#FFFFffff&lt; /item&gt;

　　&lt; item name="android:gravity"&gt;center&lt; /item&gt;

　　&lt; item name="android:layout\_marginTop"&gt;15dip&lt; /item&gt;

　　&lt; item name="android:layout\_centerHorizontal"&gt;true&lt; /item&gt;

　　&lt; item name="android:background"&gt;@drawable/selector&lt; /item&gt;

　　&lt; /style&gt;

　　&lt; style name="homeiv"&gt;

　　&lt; item name="android:layout\_width"&gt;wrap\_content&lt; /item&gt;

       &lt; item name="android:layout\_height"&gt;wrap\_content&lt; /item&gt;

　　&lt; item name="android:layout\_marginTop"&gt;15dip&lt; /item&gt;

　　&lt; item name="android:layout\_centerHorizontal"&gt;true&lt; /item&gt;

　　&lt; /style&gt;

　　&lt; /resources&gt;

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

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

　　parent ? 可选，一些在自定义的style中没有指定的属性会继承parent style中的值。parent可以是android预定义的resource，也可以是自己定义的style。

　　现在看看使用parent之后的样子：

　　Xml代码 收藏代码

　　&lt; ?xml version="1.0" encoding="utf-8"?&gt;

　　&lt; resources&gt;

　　&lt; style name="homeiv"&gt;

　　&lt; item name="android:layout\_width"&gt;wrap\_content&lt; /item&gt;

　　&lt; item name="android:layout\_height"&gt;wrap\_content&lt; /item&gt;

　　&lt; item name="android:layout\_marginTop"&gt;15dip&lt; /item&gt;

　　&lt; item name="android:layout\_centerHorizontal"&gt;true&lt; /item&gt;

　　&lt; item name="android:background"&gt;@drawable/home&lt; /item&gt;

　　&lt; /style&gt;

　　&lt; style name="homebtn" parent="@style/homeiv"&gt;

　　&lt; item name="android:textSize"&gt;20sp&lt; /item&gt;

　　&lt; item name="android:textColor"&gt;#FFFFffff&lt; /item&gt;

　　&lt; item name="android:gravity"&gt;center&lt; /item&gt;

　　&lt; item name="android:background"&gt;@drawable/selector&lt; /item&gt;

　　&lt; /style&gt;

　  &lt; /resources&gt;

　　&lt; ?xml version="1.0" encoding="utf-8"?&gt;

　　&lt; resources&gt;

　　&lt; style name="homeiv"&gt;

　　&lt; item name="android:layout\_width"&gt;wrap\_content&lt; /item&gt;

　　&lt; item name="android:layout\_height"&gt;wrap\_content&lt; /item&gt;

      &lt; item name="android:layout\_marginTop"&gt;15dip&lt; /item&gt;

　　&lt; item name="android:layout\_centerHorizontal"&gt;true&lt; /item&gt;

　　&lt; item name="android:background"&gt;@drawable/home&lt; /item&gt;

　　&lt; /style&gt;

　　&lt; style name="homebtn" parent="@style/homeiv"&gt;

　　&lt; item name="android:textSize"&gt;20sp&lt; /item&gt;

　　&lt; item name="android:textColor"&gt;#FFFFffff&lt; /item&gt;

　　&lt; item name="android:gravity"&gt;center&lt; /item&gt;

　　&lt; item name="android:background"&gt;@drawable/selector&lt; /item&gt;

　　&lt; /style&gt;

　　&lt; /resources&gt;

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