閱讀499 返回首頁    go 阿裏雲 go 技術社區[雲棲]


Android使用XML的方式為背景添加漸變,描邊,圓角的效果

使用XML的方式為背景添加漸變的效果
首先,在res/Drawable 文件夾裏麵添加一個jbshape.xml文件,然後寫入如下代碼:


shape 節點配置的是圖形的形式,主要包括方形、圓形等,上邊代碼為方形,

gradient 節點主要配置起點顏色、終點顏色及中間點的顏色、坐標、漸變效果(0,90,180從左到右漸變,270從上到下漸變)默認從左到右
padding 節點主要配置上下左右的間距

corners 節點配置四周園腳的半徑

實現這個效果,需要定一個title.xml 內容如下:

複製代碼
<?xml version=”1.0″ encoding=”UTF-8″?>
<LinearLayout xmlns:android=”https://schemas.android.com/apk/res/android”
android:orientation=”vertical”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:background=”@drawable/jbshape”

android:paddingLeft=”0px”
>
複製代碼

 

在實現背景漸變的帖子裏

https://androidturing.iteye.com/blog/1238909

有朋友建議看看shape的用法,確實很有幫助。這裏我偷懶轉一篇比較詳細的帖子,和大家一起進步~!

Android中常常使用shape來定義控件的一些顯示屬性,今天看了一些shape的使用,對shape有了大體的了解,稍作總結:
先看下麵的代碼:

複製到剪貼板  XML/HTML代碼
  1. <shape> 
  2.     <!-- 實心 --> 
  3.     <solidandroid:color="#ff9d77"/> 
  4.     <!-- 漸變 --> 
  5.     <gradient 
  6.         android:startColor="#ff8c00" 
  7.         android:endColor="#FFFFFF" 
  8.         android:angle="270"/> 
  9.     <!-- 描邊 --> 
  10.     <stroke 
  11.         android:width="2dp" 
  12.         android:color="#dcdcdc"/> 
  13.     <!-- 圓角 --> 
  14.     <corners 
  15.         android:radius="2dp"/> 
  16.     <padding 
  17.         android:left="10dp" 
  18.         android:top="10dp" 
  19.         android:right="10dp" 
  20.         android:bottom="10dp"/> 
  21. </shape> 

 

solid:實心,就是填充的意思 android:color指定填充的顏色
gradient:漸變 android:startColor和android:endColor分別為起始和結束顏色,ndroid:angle是漸變角度,必須為45的整數倍。 另外漸變默認的模式為android:type="linear",即線性漸變,可以指定漸變為徑向漸變,android:type="radial",徑向漸變需要指定半徑android:gradientRadius="50"。
stroke:描邊 android:width="2dp" 描邊的寬度,android:color 描邊的顏色。 我們還可以把描邊弄成虛線的形式,設置方式為: android:dashWidth="5dp"
android:dashGap="3dp" 其中android:dashWidth表示'-'這樣一個橫線的寬度,android:dashGap表示之間隔開的距離。
corners:圓角 android:radius為角的弧度,值越大角越圓。 我們還可以把四個角設定成不同的角度,方法為:

XML/HTML代碼
  1. <corners 
  2.  
  3.         android:topRightRadius="20dp"    右上角 
  4.         android:bottomLeftRadius="20dp"    右下角 
  5.         android:topLeftRadius="1dp"    左上角 
  6.         android:bottomRightRadius="0dp"    左下角 
  7. /> 

這裏有個地方需要注意,bottomLeftRadius是右下角,而不是左下角,這個有點鬱悶,不過不影響使用,記得別搞錯了就行。 還有網上看到有人說設置成0dp無效,不過我在測試中發現是可以的,我用的是2.2,可能修複了這個問題吧,如果無效的話那就隻能設成1dp了。
padding:間隔 這個就不用多說了,XML布局文件中經常用到。
大體的就是這樣,以下是一個使用的具體示例:用在Selector中作為Button的背景,分別定義了按鈕的一般狀態、獲得焦點狀態和按下時的狀態,具體代碼如下:

 

 XML/HTML代碼
  1. main.xml: 
  2. <Button 
  3.     android:layout_width="wrap_content" 
  4.     android:layout_height="wrap_content" 
  5.     android:text="TestShapeButton" 
  6.     android:background="@drawable/button_selector" 
  7.     /> 
  8. > 

button_selector.xml:

 

XML/HTML代碼
  1. <?xmlversion="1.0"encoding="utf-8"?> 
  2. <selector 
  3.     xmlns:android="https://schemas.android.com/apk/res/android"> 
  4.     <itemandroid:state_pressed="true"> 
  5.         <shape> 
  6.             <!-- 漸變 --> 
  7.             <gradient 
  8.                 android:startColor="#ff8c00" 
  9.                 android:endColor="#FFFFFF" 
  10.                 android:type="radial" 
  11.                 android:gradientRadius="50"/> 
  12.             <!-- 描邊 --> 
  13.             <stroke 
  14.                 android:width="2dp" 
  15.                 android:color="#dcdcdc" 
  16.                 android:dashWidth="5dp" 
  17.                 android:dashGap="3dp"/> 
  18.             <!-- 圓角 --> 
  19.             <corners 
  20.                 android:radius="2dp"/> 
  21.             <padding 
  22.                 android:left="10dp" 
  23.                 android:top="10dp" 
  24.                 android:right="10dp" 
  25.                 android:bottom="10dp"/> 
  26.         </shape> 
  27.     </item> 
  28.     <itemandroid:state_focused="true"> 
  29.         <shape> 
  30.             <gradient 
  31.                 android:startColor="#ffc2b7" 
  32.                 android:endColor="#ffc2b7" 
  33.                 android:angle="270"/> 
  34.             <stroke 
  35.                 android:width="2dp" 
  36.                 android:color="#dcdcdc"/> 
  37.             <corners 
  38.                 android:radius="2dp"/> 
  39.             <padding 
  40.                 android:left="10dp" 
  41.                 android:top="10dp" 
  42.                 android:right="10dp" 
  43.                 android:bottom="10dp"/> 
  44.         </shape> 
  45.     </item> 
  46.     <item>       
  47.         <shape> 
  48.             <solidandroid:color="#ff9d77"/> 
  49.             <stroke 
  50.                 android:width="2dp" 
  51.                 android:color="#fad3cf"/> 
  52.             <corners 
  53.                 android:topRightRadius="5dp" 
  54.                 android:bottomLeftRadius="5dp" 
  55.                 android:topLeftRadius="0dp" 
  56.                 android:bottomRightRadius="0dp" 
  57.             /> 
  58.             <padding 
  59.                 android:left="10dp" 
  60.                 android:top="10dp" 
  61.                 android:right="10dp" 
  62.                 android:bottom="10dp"/> 
  63.         </shape> 
  64.     </item> 
  65. </selector> 

 

運行效果如下圖: 一般狀態: 
獲得焦點狀態: 
按下狀態: 

最後更新:2017-04-02 16:47:54

  上一篇:go 互聯網協議入門
  下一篇:go Android自定義密碼鍵盤