国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android中Shape的用法詳解

2019-10-22 18:32:46
字體:
來源:轉載
供稿:網友

ShapeDrawable是一種很常見的Drawable,可以理解為通過顏色來構造的圖形,它既可以是純色的圖形,也可以是具有漸變效果的圖形,ShapeDrawabled語法稍顯復雜,如下所示:

<shape  xmlns:android="http://schemas.android.com/apk/res/android"  android:shape=["rectangle" | "oval" | "line" | "ring"] >  <corners    android:radius="integer"    android:topLeftRadius="integer"    android:topRightRadius="integer"    android:bottomLeftRadius="integer"    android:bottomRightRadius="integer" />  <gradient    android:angle="integer"    android:centerX="integer"    android:centerY="integer"    android:centerColor="integer"    android:endColor="color"    android:gradientRadius="integer"    android:startColor="color"    android:type=["linear" | "radial" | "sweep"]    android:useLevel=["true" | "false"] />  <padding    android:left="integer"    android:top="integer"    android:right="integer"    android:bottom="integer" />  <size    android:width="integer"    android:height="integer" />  <solid    android:color="color" />  <stroke    android:width="integer"    android:color="color"    android:dashWidth="integer"    android:dashGap="integer" /></shape>

•Android: shape

•有4個選項,rectangle(矩形)oval(橢圓)line(橫線)ring(圓環),默認為rectangle,需要注意line和ring需要通過標簽來指定線的寬度和顏色等信息,否則無法達到預期效果

•首先來說一下最常用的rectangle(矩形),一般都是在按鈕或者字體上面設置一個background的Drawable。一般設置效果為正方形或者兩邊有弧度的形狀。

?第一種情況就是設置矩形背景

<shape xmlns:android="http://schemas.android.com/apk/res/android"  android:shape="rectangle"  >  <size  android:width="200dp"  android:height="20dp"  />  <solid android:color="#d22121"/>  </shape> 

通過設置size設置矩形的寬度和高度,*這里需要說明一下,咱們在這里設置size的寬高,在最終顯示尺寸是沒有用的,也就是說當你在一個控件中設置background的時候,這個shape是會被拉伸或者縮小為view的大小。*solid屬性設置矩形里面的背景顏色。

android,shape,用法

將背景色設置為漸變

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"  >  <size  android:width="200dp"  android:height="20dp"  />  <gradient  android:startColor="#fff"  android:centerColor="#f1a9a9"  android:endColor="#ec5b5b"  android:type="linear"  />  </shape>

效果圖:

android,shape,用法

這里默認的type就是linear,里面還有其他兩個屬性可以選擇分別是radial(徑向漸變)和sweep(掃描漸變)
一般最常用的也就是線性漸變還有其他幾個屬性沒有用但是很好理解

android:angle——漸變的角度,默認為0,其值必須是45的倍數,0表示從左到右,90表示從下到上。

android:centerX——漸變的中心點橫坐標

android:centerY——漸變的中心點縱坐標

android:gradientRadiu——漸變半徑,僅當android:type=”radial”時有效

•接下來說一下畫圓角的矩形背景

?其實只用設置一下corners的屬性就是了。

android,shape,用法

?具體詳細的說明

?android:radius—— 給四個角設置相同的角度,優先級較低,會被其他四個屬性覆蓋

android:bottomLeftRadius——設定左下角的角度

android:bottomRightRadius——設定右下角的角度

android:TopLeftRadius——設定左上角的角度

android:TopRightRadius——設定右上角的角度

接下來就是如何畫一個空心的背景

<shape xmlns:android="http://schemas.android.com/apk/res/android"  android:shape="rectangle"  >  <size  android:width="200dp"  android:height="20dp"  />  <stroke  android:width="1px"  android:color="#ffff1c77"  />  <corners android:radius="50dp"/>  </shape> 

效果圖如下

android,shape,用法

 當然里面也可以自由發揮設置漸變色,但是一般里面都純色。

?這里里面也可以設置為虛線

<shape xmlns:android="http://schemas.android.com/apk/res/android"  android:shape="rectangle"  >  <size  android:width="200dp"  android:height="20dp"  />  <stroke  android:dashWidth="4dp"  android:dashGap="2dp"  android:width="1px"  android:color="#ffff1c77"  />  <corners android:radius="50dp"/>  </shape>

android,shape,用法

好了,其實里面的東西很簡單,總結一下就好了。希望大家用的開心。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 紫云| 黄龙县| 略阳县| 绥德县| 北票市| 赫章县| 保定市| 涞水县| 成都市| 邓州市| 马尔康县| 宽城| 金溪县| 海安县| 云安县| 安阳县| 伊宁县| 德州市| 潜山县| 于田县| 平邑县| 济南市| 沁源县| 江口县| 南岸区| 阿拉善左旗| 平果县| 乌兰察布市| 潼南县| 历史| 信阳市| 临海市| 措美县| 神木县| 栾城县| 阿拉尔市| 松原市| 逊克县| 武平县| 柳林县| 阳城县|