Android Shape Drawable Resources TextView漸變 背景色 圓角

轉自http://blog.csdn.net/luckkof

正文

 

本文主要介紹Drawable Resources的一種,Shape Drawable Resources的使用。其他Drawable類似

經常需要自己設置某個view的背景,比如類似新浪微博客戶端微博源內容的灰底圓角效果,這個時候我們就可以使用Shape。

1、介紹

Shape Drawable Resources是指一個XML文件,它定義了幾何形狀,包括顏色和漸變

放在res/Drawable文件夾下,文件名即爲資源id,可以在其他layout中調用R.drawable.filename,

對應的類爲ShapeDrawable

shape包含矩形、橢圓形、行、環形。

2、使用

下面以爲一個TextView設置一個漸變色的邊框爲例進行介紹,第三部分對具體屬性含義進行介紹

2.1 定義一個漸變色的矩形shape,文件路徑res/drawable/gradient_box.xml

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:shape="rectangle"> 
  4.     <gradient 
  5.         android:startColor="#FFFF0000" 
  6.         android:endColor="#80FF00FF" 
  7.         android:angle="45"/> 
  8.     <padding android:left="7dp" 
  9.         android:top="7dp" 
  10.         android:right="7dp" 
  11.         android:bottom="7dp" /> 
  12.     <corners android:radius="8dp" /> 
  13. </shape> 
2.2 TextView屬性設置

  1. <TextView 
  2.     android:background="@drawable/gradient_box" 
  3.     android:layout_height="wrap_content" 
  4.     android:layout_width="wrap_content" /> 

其中 android:background="@drawable/gradient_box"表示設置背景爲 gradient_box 這個drawable

或者在後臺程序中設置

  1. Resources res = getResources(); 
  2. Drawable shape = res. getDrawable(R.drawable.gradient_box); 
  3.  
  4. TextView tv = (TextView)findViewByID(R.id.textview); 
  5. tv.setBackground(shape); 

3、屬性介紹

xml定義如下

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <shape 
  3.     xmlns:android="http://schemas.android.com/apk/res/android" 
  4.     android:shape=["rectangle" | "oval" | "line" | "ring"] > 
  5.     <corners 
  6.         android:radius="integer" 
  7.         android:topLeftRadius="integer" 
  8.         android:topRightRadius="integer" 
  9.         android:bottomLeftRadius="integer" 
  10.         android:bottomRightRadius="integer" /> 
  11.     <gradient 
  12.         android:angle="integer" 
  13.         android:centerX="integer" 
  14.         android:centerY="integer" 
  15.         android:centerColor="integer" 
  16.         android:endColor="color" 
  17.         android:gradientRadius="integer" 
  18.         android:startColor="color" 
  19.         android:type=["linear" | "radial" | "sweep"] 
  20.         android:useLevel=["true" | "false"] /> 
  21.     <padding 
  22.         android:left="integer" 
  23.         android:top="integer" 
  24.         android:right="integer" 
  25.         android:bottom="integer" /> 
  26.     <size 
  27.         android:width="integer" 
  28.         android:height="integer" /> 
  29.     <solid 
  30.         android:color="color" /> 
  31.     <stroke 
  32.         android:width="integer" 
  33.         android:color="color" 
  34.         android:dashWidth="integer" 
  35.         android:dashGap="integer" /> 
  36. </shape> 

其中shape必須爲根元素,android:shape定義了形狀,默認爲矩形。

corners只對矩形有效,表示圓角的度數

gradient表示漸變色

padding表示即對內的偏移

size爲shape大小

solid爲填充色

stroke爲shape邊線的設置

以上屬性的自屬性含義見drawable-Shape,更多可以使用的屬性見GradientDrawable.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章