歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android應用開發之樣式

Android應用開發之樣式

日期:2017/3/1 10:27:57   编辑:Linux編程

Android中的樣式和CSS樣式作用相似,都是用於為界面元素定義顯示風格,它是一個包含一個或者多個view控件屬性的集合。如:需要定義字體的顏色和大小。

在CSS中是這樣定義的:

  1. <style>
  2. .itcast{
  3. COLOR:#0000CC;
  4. font-size:18px;
  5. }
  6. </style>

可以像這樣使用上面的css樣式:

  1. <div class="itcast">傳智播客</div>

在Android中可以這樣定義樣式:

在res/values/styles.xml文件中添加以下內容

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <style name=“itcast”> <!-- 為樣式定義一個全局唯一的名字-->
  4. <!-- name屬性為樣式要用在的View控件持有的屬性 -->
  5. <item name="android:textSize">18px</item>
  6. <item name="android:textColor">#0000CC</item>
  7. </style>
  8. </resources>

在layout文件中可以像下面這樣使用上面的android樣式:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ....>
  3. <TextView style="@style/itcast"
  4. ..... />
  5. </LinearLayout>

實驗

values/styles.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <style name="mystyle">
  4. <item name="android:layout_width">fill_parent</item>
  5. <item name="android:textSize">20dip</item>
  6. </style>
  7. <style name="submystyle" parent="mystyle">
  8. <item name="android:layout_width">wrap_content</item>
  9. <item name="android:textColor">#FF0000</item>
  10. <item name="android:gravity">center</item>
  11. <item name="android:padding">10dip</item>
  12. </style>
  13. </resources>

main.xml

  1. <TextView
  2. style="@style/mystyle"
  3. android:layout_height="wrap_content"
  4. android:text="@string/hello" />
  5. <TextView
  6. style="@style/submystyle"
  7. android:layout_height="wrap_content"
  8. android:text="HVGA:480x320" />
Copyright © Linux教程網 All Rights Reserved