1. 程式人生 > >Android Shape Drawable Resources TextView漸變 背景色 圓角

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

正文

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

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

1、介紹

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

放在res/Drawable資料夾下,檔名即為資源id,可以在其他layout中呼叫R.drawable.filename,

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

2、使用

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

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

[html] view plaincopyprint?在CODE上檢視程式碼片派生到我的程式碼片
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <shapexmlns: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.     <paddingandroid:left="7dp"
  9.         android:top="7dp"
  10.         android:right="7dp"
  11.         android:bottom="7dp"/>
  12.     <cornersandroid:radius="8dp"/>
  13. </shape>
2.2 TextView屬性設定 [html] view plaincopyprint?在CODE上檢視程式碼片派生到我的程式碼片
  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

或者在後臺程式中設定

[html] view plaincopyprint?在CODE上檢視程式碼片派生到我的程式碼片
  1. Resources res = getResources(); 
  2. Drawable shape = res. getDrawable(R.drawable.gradient_box); 
  3. TextView tv = (TextView)findViewByID(R.id.textview); 
  4. tv.setBackground(shape); 

3、屬性介紹

xml定義如下

[html] view plaincopyprint?在CODE上檢視程式碼片派生到我的程式碼片
  1. <?xmlversion="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邊線的設定