android自定義圓角實線邊框,圓角虛線邊框,直實線,虛實線,半圓角邊框


先上圖

在現實項目開發中,單純的Button,EditText等控件遠遠不能滿足我們項目的UI設計需求,這時候,我們就需要自己動手豐衣足食啦。接下來先給大家介紹一些屬性,備注寫的都非常清楚啦,我就不啰嗦啦。
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <!--android:shape屬性代表繪制的圖形形狀 retangle;矩形,oval:橢圓 ,line:線 ring,環形-->
 3 <shape xmlns:android="http://schemas.android.com/apk/res/android"
 4     android:shape="###">
 5 
 6     <!--stroke主要給我們所要畫的圖形進行描邊 color:邊框顏色,width:邊框寬度,dashGap:虛線框的間隔,dashWidth:虛線框的寬度-->
 7     <stroke
 8         android:width="###"
 9         android:color="###"
10         android:dashGap="###"
11         android:dashWidth="###" />
12 
13     <!--corners主要是設置我們所畫圖形四個角的半徑 radius:四角半徑 bottomLeftRadius:左下角半徑,
14     bottomRightRadius:右下角半徑,topLeftRadius:左上角半徑,topRightRadius:右上角半徑-->
15     <corners
16         android:bottomLeftRadius="###"
17         android:bottomRightRadius="###"
18         android:radius="###"
19         android:topLeftRadius="###"
20         android:topRightRadius="###" />
21 
22     <!--padding主要設置內邊距,也就是你裝載的內容(大部分是Textview或者button)離圖形邊框的距離
23     bottom:下內邊距,left:左內邊距,right:右內邊距,top:上內邊距-->
24     <padding
25         android:bottom="###"
26         android:left="###"
27         android:right="###"
28         android:top="###" />
29 
30     <!--這個就不需要講了吧-->
31     <size
32         android:width="###"
33         android:height="###" />
34     <!--主要設置你所畫圖形的填充色-->
35     <solid
36         android:color="###"/>
37     <!--gradient主要指定一個漸變顏色的形狀。-->
38     <gradient
39         android:angle="###"
40         android:centerColor="###"
41         android:centerX="###"
42         android:centerY="###"
43         android:gradientRadius="###"
44         android:endColor="###"
45         android:startColor="###"
46         android:type="###"
47         android:useLevel="###"/>
48 </shape>

接下來我們看最頂上的"哈哈"與"嘻嘻"。通過corners設置左下角和左上角的半徑為5dp,右上角,右下角半徑為0dp,我們就可以得到左邊圓角,右邊直角的邊框啦。

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
 3 
 4     <item>
 5         <shape android:shape="rectangle">
 6             <stroke
 7                 android:width="1.2dp"
 8                 android:color="#6f4189" />
 9 
10             <solid android:color="#6f4189" />
11             <corners
12                 android:bottomLeftRadius="5dp"
13                 android:bottomRightRadius="0dp"
14                 android:topLeftRadius="5dp"
15                 android:topRightRadius="0dp" />
16 
17             <padding
18                 android:bottom="2dp"
19                 android:left="12dp"
20                 android:right="12dp"
21                 android:top="2dp" />
22         </shape>
23     </item>
24 
25 </layer-list>

下面一樣,通過corners設置右下角和右上角的半徑為5dp,左上角,左下角半徑為0dp,我們即可得到左邊直角,右邊圓角的邊框。

 1 <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
 2 
 3     <item>
 4         <shape>
 5             <stroke
 6                 android:width="1.2dp"
 7                 android:color="#6f4189" />
 8             <corners
 9                 android:bottomLeftRadius="0dp"
10                 android:bottomRightRadius="5dp"
11                 android:topLeftRadius="0dp"
12                 android:topRightRadius="5dp" />
13             <solid android:color="#00000000" />
14             <padding
15                 android:bottom="2dp"
16                 android:left="12dp"
17                 android:right="12dp"
18                 android:top="2dp" />
19         </shape>
20     </item>
21 
22 </layer-list>

它倆再加上viewpager就可以實現很多App上都有的左右滑動翻頁效果啦。

 

 

我們再看圖中的用戶名和密碼輸入框,至於整個框框就不說啦,和上面的'嘻嘻','哈哈'一個原理,主要給大家介紹一下中間的紅線。實現很簡單,我們只需要設置android:shape="line",然后通過stoke的android:width設置直線的寬度,android;color設置直線的顏色即可。

1 <?xml version="1.0" encoding="utf-8"?>
2 <shape xmlns:android="http://schemas.android.com/apk/res/android"
3     android:shape="line">
4    <stroke
5        android:width="1.2dp"
6        android:color="#ff4323"/>
7 </shape>

讓其在頁面的顯示代碼如下

1 <LinearLayout
2             android:id="@+id/straight_line"
3             android:layout_width="fill_parent"
4             android:layout_height="2dp"
5             android:background="@drawable/line"
6             android:orientation="vertical"/>

其實設置直線還有種跟直觀的方法,通過<view/>來設置,在這里就不細講,大家可以自行百度。

 

 

接下來我們看看下面的三個登錄框框,重點給大家講講最后面那個"斷點"虛線框框。下面最后一個代碼模塊是"斷點"虛線框框的代碼,其中color是定義虛線的顏色,dashGap定義的是虛線的間隔,width定義的是虛線的大小,dashWidth定義虛線的寬度。

1 <?xml version="1.0" encoding="utf-8"?>
2 <shape xmlns:android="http://schemas.android.com/apk/res/android"
3     android:shape="rectangle">
4     <corners
5         android:radius="5dp"/>
6     <solid
7         android:color="#E67e22"/>
8 </shape>
1 <?xml version="1.0" encoding="utf-8"?>
2 <shape xmlns:android="http://schemas.android.com/apk/res/android"
3     android:shape="rectangle">
4     <corners
5         android:radius="5dp"/>
6     <stroke
7         android:color="#E67e22"
8         android:width="1.0dp"/>
9 </shape>
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <shape xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:shape="rectangle">
 4     <corners android:radius="5dp" />
 5     <stroke
 6         android:color="#E67e22"
 7         android:dashGap="5dp"
 8         android:width="2dp"
 9         android:dashWidth="1.0dp" />
10 </shape>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM