---
title: "style的使用"
author: "Clareshao"
date: "2010-10-28T13:38:00Z"
tags: []
categories: ["移动开发"]
slug: "style"
draft: false
lastmod: "2010-10-28T13:41:15Z"
id: 796469
url: "https://clareshao.iteye.com/blog/796469"
---

在xml中定义布局时可能定义的很多组件的属性是相同的，此时可以使用style来减少代码量，以下是一个简单的例子：

（1）在res\\values下创建style.xml文件来存放样式：

&lt;?xml version="1.0" encoding="utf-8"?&gt;  
&lt;resources&gt;  
 &lt;style name="myStyle\_Text1"&gt;  
  &lt;item name="android:textSize"&gt;25sp&lt;/item&gt;  
  &lt;item name="android:textColor"&gt;#80FF00&lt;/item&gt;  
 &lt;/style&gt;  
 &lt;style name="myStyle\_Text2"&gt;  
  &lt;item name="android:textSize"&gt;18sp&lt;/item&gt;"  
  &lt;item name="android:textColor"&gt;#0C688E&lt;/item&gt;  
 &lt;/style&gt;  
&lt;/resources&gt;

 

（2）在布局文件main.xml中使用style：

&lt;?xml version="1.0" encoding="utf-8"?&gt;  
&lt;LinearLayout xmlns:android="[http://schemas.android.com/apk/res/android](http://schemas.android.com/apk/res/android)"  
    android:orientation="vertical"  
    android:layout\_width="fill\_parent"  
    android:layout\_height="fill\_parent"  
    &gt;  
&lt;TextView   
 android:id="@+id/TextView01"  
 style="@style/myStyle\_Text1"  
    android:layout\_width="fill\_parent"  
    android:layout\_height="wrap\_content"  
    android:gravity="center\_vertical|center\_horizontal"  
    android:text="应用myStyle\_Text1"  
    /&gt;  
     
&lt;TextView   
 android:id="@+id/TextView02"  
 style="@style/myStyle\_Text2"  
    android:layout\_width="fill\_parent"  
    android:layout\_height="wrap\_content"  
    android:gravity="center\_vertical|center\_horizontal"  
    android:text="应用myStyle\_Text2"  
    /&gt;     
&lt;/LinearLayout&gt;

 

 

 具体代码见附件