日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android 基础(十三) shape

發(fā)布時(shí)間:2025/3/15 Android 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android 基础(十三) shape 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

介紹

簡單來說,shape就是用來在xml文件中定義形狀,代碼解析之后就可以當(dāng)做Drawable一樣使用

官方說明

關(guān)于shape定義的drawable

文件位置:res/drawable/filename.xml

編譯資源類型:GradientDrawable

文件引用:
In Java: R.drawable.filename
In XML: @[package:]drawable/filename

語法:

<?xml version="1.0" encoding="utf-8"?> <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>

這里只做簡單的描述,主要看看使用方式。關(guān)于元素的詳細(xì)說明,請看 shape說明

實(shí)際使用

矩形

填充(solid)

<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><!--填充顏色 --><solid android:color="@color/colorAccent" /> </shape>


描邊(stroke)

<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><!--填充顏色 --><solid android:color="@color/colorAccent" /><!--描邊顏色--><!--android:dashGap 虛線間距,這里設(shè)置為0則顯示的為實(shí)線--><!--android:dashWidth 虛線寬度--><stroke android:width="3dp"android:color="@color/colorPrimaryDark"android:dashGap="0dp"android:dashWidth="10dp" /> </shape>


圓角(corner)

<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><!--填充顏色 --><solid android:color="@color/colorAccent" /><!--描邊顏色--><!--android:dashGap 虛線間距,這里設(shè)置為0則顯示的為實(shí)線--><!--android:dashWidth 虛線寬度--><stroke android:width="3dp"android:color="@color/colorPrimaryDark"android:dashGap="4dp"android:dashWidth="10dp" /><!--圓角--><!--android:randius 設(shè)置4個(gè)叫的圓角半徑,會(huì)被特定的圓角設(shè)定覆蓋--><!--android:bottomLeftRadius 左下角的圓角半徑--><!--android:bottomRightRadius 右下角的圓角半徑--><!--android:topLeftRandius 左上角的圓角半徑--><!--android:topRightRadius 右上角的圓角半徑--><corners android:bottomLeftRadius="60dp"android:radius="30dp"android:topRightRadius="120dp" /> </shape>


漸變(gradient):linear

<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><!--描邊顏色--><!--android:dashGap 虛線間距,這里設(shè)置為0則顯示的為實(shí)線--><!--android:dashWidth 虛線寬度--><stroke android:width="3dp"android:color="@color/colorPrimaryDark"android:dashGap="4dp"android:dashWidth="10dp" /><!--圓角--><!--android:randius 設(shè)置4個(gè)叫的圓角半徑--><!--android:bottomLeftRadius 左下角的圓角半徑--><!--android:bottomRightRadius 右下角的圓角半徑--><!--android:topLeftRandius 左上角的圓角半徑--><!--android:topRightRadius 右上角的圓角半徑--><corners android:bottomLeftRadius="60dp"android:radius="30dp"android:topRightRadius="120dp" /><!--漸變--><gradient android:angle="45"android:centerColor="@color/stone"android:endColor="@color/pink"android:startColor="@color/yellow" /></shape>

漸變(gradient):radial

<gradientandroid:angle="90"android:startColor="@color/colorPrimary"android:centerColor="@color/pink"android:endColor="@color/yellow"android:gradientRadius="400dp"android:type="radial"/>

漸變(gradient):sweep

<gradientandroid:startColor="@color/colorPrimary"android:centerColor="@color/pink"android:endColor="@color/yellow"android:gradientRadius="400dp"android:type="sweep"/>


圓形

正圓

<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <gradient android:centerColor="@color/pink"android:endColor="@color/yellow"android:startColor="@color/colorPrimary" /> <size android:width="400dp"android:height="400dp" /> </shape>


橢圓

<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <gradient android:angle="90"android:centerColor="@color/pink"android:endColor="@color/yellow"android:startColor="@color/colorPrimary" /> </shape>


線條

<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="line"><stroke android:width="9dp"android:color="@color/pink" /> </shape>


環(huán)形

<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="ring"android:innerRadius="100dp"android:thickness="50dp"android:useLevel="false"><gradient android:startColor="@color/colorAccent"android:endColor="@color/yellow"android:centerColor="@color/pink"/> </shape>

android:useLevel=”false”這個(gè)屬性值一定要設(shè)置成false,根據(jù)google官網(wǎng)上的解釋:

以下屬性只能在android:shape="ring"的時(shí)候使用:

屬性意義
android:innerRadius尺寸,內(nèi)環(huán)的半徑
android:thickness尺寸,環(huán)的厚度
android:innerRadiusRatio浮點(diǎn)型,以環(huán)的寬度比率來表示內(nèi)環(huán)的半徑, 例如,如果android:innerRadiusRatio=5,表示內(nèi)環(huán)半徑等于環(huán)的寬度除以5,這個(gè)值是可以被android:innerRadius的值覆蓋,默認(rèn)為9.
android:thicknessRatio浮點(diǎn)型,以環(huán)的寬度比率來表示環(huán)的厚度,例如,如果android:thicknessRatio=2, 那么環(huán)的厚度就等于環(huán)的寬度除以2。這個(gè)值是可以被android:thickness覆蓋的,默認(rèn)值是3.
android:useLevelboolean值,如果當(dāng)做是LevelListDrawable使用時(shí)值為true,否則為false.這個(gè)值一般為false,否則你的環(huán)可能不會(huì)出現(xiàn)

其他說明

這些自己定義的shape為根節(jié)點(diǎn)的drawable xml文件,可以用來當(dāng)成背景使用在Button,TextView等視圖

上,同時(shí)由于可以設(shè)置size大小,也可以用來制作簡單的圖標(biāo)等。總而言之,每個(gè)細(xì)小的東西,都有挖掘的

價(jià)值,感覺這里面還有一些東西我沒有注意到,還要好好的看一下文檔。

最后,google鏡像網(wǎng)站,xsoftlab,當(dāng)然有條件的建議使用Google官網(wǎng)。

轉(zhuǎn)載于:https://www.cnblogs.com/lanzhi/p/6467191.html

總結(jié)

以上是生活随笔為你收集整理的Android 基础(十三) shape的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。