Android Shape 的使用

目錄

什么是Shape?

shape屬性

子標簽屬性

corners (圓角)

solid (填充色)

gradient (漸變)

stroke (描邊)

padding (內邊距)

size (大小)

特殊屬性

rectangle(矩形)

oval(橢圓)

line(線)

ring(圓環)

shape 用法


什么是Shape?

在Android開發中,我們可以使用shape定義各種各樣的形狀,也可以定義一些圖片資源。相對于傳統圖片來說,使用shape可以減少資源占用,減少安裝包大小,還能夠很好地適配不同尺寸的手機。

shape屬性

shape屬性的基本語法示例

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

子標簽屬性

Shape可以定義控件的一些展示效果,例如圓角,漸變,填充,描邊,大小,邊距; shape 子標簽就可以實現這些效果,shape 子標簽有下面幾個屬性:corners,gradient,padding,size,solid,stroke

corners (圓角)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" ><corners //定義圓角android:radius="10dp" //全部的圓角半徑;android:topLeftRadius="5dp" //左上角的圓角半徑;android:topRightRadius="5dp" //右上角的圓角半徑;android:bottomLeftRadius="5dp" //左下角的圓角半徑;android:bottomRightRadius="5dp" /> //右下角的圓角半徑。
</shape>

solid (填充色)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" ><solid android:color="#ffff00"/> //內部填充色
</shape>

gradient (漸變)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" ><gradientandroid:type=["linear" | "radial" | "sweep"] //共有3中漸變類型,線性漸變(默認)/放射漸變/掃描式漸變;android:angle="90" //漸變角度,必須為45的倍數,0為從左到右,90為從上到下;android:centerX="0.5" //漸變中心X的相當位置,范圍為0~1;android:centerY="0.5" //漸變中心Y的相當位置,范圍為0~1;android:startColor="#24e9f2" //漸變開始點的顏色;android:centerColor="#2564ef" //漸變中間點的顏色,在開始與結束點之間;android:endColor="#25f1ef" //漸變結束點的顏色;android:gradientRadius="5dp" //漸變的半徑,只有當漸變類型為radial時才能使用;android:useLevel="false" /> //使用LevelListDrawable時就要設置為true。設為false時才有漸變效果。
</shape>

stroke (描邊)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" ><strokeandroid:width="1dp" //描邊的寬度android:color="#ff0000" //描邊的顏色// 以下兩個屬性設置虛線android:dashWidth="1dp" //虛線的寬度,值為0時是實線android:dashGap="1dp" />//虛線的間隔
</shape>

padding (內邊距)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" ><paddingandroid:left="10dp" //左內邊距;android:top="10dp" //上內邊距;android:right="10dp" //右內邊距;android:bottom="10dp" /> //下內邊距。
</shape>

size (大小)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" ><sizeandroid:width="50dp" //寬度android:height="50dp" />// 高度
</shape>

特殊屬性

Shape可以定義當前Shape的形狀的,比如矩形,橢圓形,線形和環形;這些都是通過 shape 標簽屬性來定義的, shape 標簽有下面幾個屬性:rectangle,oval,line,ring

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape=["rectangle" | "oval" | "line" | "ring"] //shape的形狀,默認為矩形,可以設置為矩形(rectangle)、橢圓形(oval)、線性形狀(line)、環形(ring)//下面的屬性只有在android:shape="ring"時可用:android:innerRadius="10dp" // 內環的半徑;android:innerRadiusRatio="2" // 浮點型,以環的寬度比率來表示內環的半徑;android:thickness="3dp" // 環的厚度;android:thicknessRatio="2" // 浮點型,以環的寬度比率來表示環的厚度;android:useLevel="false"> // boolean值,如果當做是LevelListDrawable使用時值為true,否則為false。
</shape>

rectangle(矩形)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><solid android:color="@color/colorPrimary"/>
</shape>

oval(橢圓)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="oval"><solid android:color="@color/colorPrimary"/><size android:height="100dp"android:width="100dp"/>
</shape>

line(線)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="line"><strokeandroid:width="1dp"android:color="@color/colorAccent"android:dashGap="3dp"//虛線間距android:dashWidth="4dp"/>//虛線寬度<size android:height="3dp"/>
</shape>

ring(圓環)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="ring"android:useLevel="false"android:innerRadius="20dp" // 內環的半徑android:thickness="10dp"> // 圓環寬度<!--useLevel需要設置為false--><solid android:color="@color/colorAccent"/>
</shape>

shape 用法

1.?在res/drawable下新建 shape_text.xml 文件

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><cornersandroid:radius="5dp"android:topLeftRadius="15dp"android:topRightRadius="15dp"android:bottomLeftRadius="15dp"android:bottomRightRadius="15dp" /><gradientandroid:startColor="#FF0000"android:endColor="#80FF00"android:angle="45" /><paddingandroid:left="10dp"android:top="10dp"android:right="10dp"android:bottom="10dp" /><sizeandroid:width="200dp"android:height="200dp" /><solid android:color="#ffff9d" /><strokeandroid:width="2dp"android:color="#dcdcdc" />
</shape>

2.?在布局中引用 shape_text.xml 文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Shape測試"android:background="@drawable/shape_text"android:textSize="15sp"android:textColor="@android:color/black"/>
</LinearLayout>

shape 使用示例

在src-main-res-drawable下,右鍵 New-Drawable Resource File

會生成一個這樣的文件

然后在上面代碼中找一個示例

然后在我們的 layout 文件中使用 shape,使用效果圖如下

?

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/38654.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/38654.shtml
英文地址,請注明出處:http://en.pswp.cn/news/38654.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

CentOS系統環境搭建(三)——Centos7安裝DockerDocker Compose

centos系統環境搭建專欄&#x1f517;點擊跳轉 Centos7安裝Docker&Docker Compose 使用 yum 安裝Docker 內核 [rootVM-4-17-centos ~]# uname -r 3.10.0-1160.88.1.el7.x86_64Docker 要求 CentOS 系統的內核版本高于 3.10 更新 yum yum update安裝需要的軟件包&#x…

在Windows Server 2008上啟用自動文件夾備份

要在Windows Server 2008上啟用自動文件夾備份&#xff0c;您可以使用內置的Windows備份功能。下面是如何設置它的方法&#xff1a; 1. 點擊“開始”按鈕并選擇“服務器管理器”&#xff0c;打開“服務器管理器”。 2. 在“服務器管理器”窗口中&#xff0c;單擊左側窗格中的“…

Python學習筆記_基礎篇(六)_Set集合,函數,深入拷貝,淺入拷貝,文件處理

1、Set基本數據類型 a、set集合&#xff0c;是一個無序且不重復的元素集合 class set(object):"""set() -> new empty set objectset(iterable) -> new set objectBuild an unordered collection of unique elements."""def add(self, *a…

redis-數據類型及樣例

一.string 類型數據的基本操作 1.添加/修改數據 set key value2.獲取數據 get key3.刪除數據 del key4.添加/修改多個數據 mset key1 value1 key2 value25.獲取多個數據 mget key1 key2二.list類型的基本操作 數據存儲需求&#xff1a;存儲多個數據&#xff0c;并對數據…

day 0815

計算文件有多少行&#xff1f; 2.文件的拷貝

SpringBoot引入外部jar打包失敗解決,SpringBoot手動引入jar打包war后報錯問題

前言 使用外部手動添加的jar到項目&#xff0c;打包時出現jar找不到問題解決 處理 例如項目結構如下 引入方式換成這種 <!-- 除了一下這兩種引入外部jar&#xff0c;還是可以將外部jar包添加到maven中&#xff08;百度查&#xff09;--><!-- pdf轉word --><…

Installshield軟件項目打包學習

Installshield打包學習記錄 個人工作學習的一點點記錄&#xff0c;可能有不專業的表述&#xff0c;各位可以提出建議&#xff0c;共同學習。 目錄 Installshield打包學習記錄一、Installshield的幾個事件&#xff1a;1. Before Move Data&#xff08;安裝數據前&#xff09;1.…

前端代理配置

dev: {env: require(./dev.env),port: process.env.PORT || 8080,autoOpenBrowser: true,assetsSubDirectory: static,assetsPublicPath: /,proxyTable: {// 以 /party/fundamental/ 開頭的請求&#xff0c;全部轉發到 target 設置的地址/party/fundamental/: {// target: http…

【BASH】回顧與知識點梳理(二十八)

【BASH】回顧與知識點梳理 二十八 二十八. 例行性工作排程(crontab)28.1 什么是例行性工作排程Linux 工作排程的種類&#xff1a; at, cronCentOS Linux 系統上常見的例行性工作 28.2 僅執行一次的工作排程atd 的啟動at 的運作方式實際運作單一工作排程at 工作的管理batch&…

Windows下升級jdk1.8小版本

1.首先下載要升級jdk最新版本&#xff0c;下載地址&#xff1a;Java Downloads | Oracle 中國 2.下載完畢之后&#xff0c;直接雙擊下載完畢后的文件&#xff0c;進行安裝。 3.安裝完畢后&#xff0c;調整環境變量至新安裝的jdk位置 4.此時&#xff0c;idea啟動項目有可能會出…

ATF bl1 ufshc_dme_get/set處理流程分析

ATF bl1 ufshc_dme_get/set處理流程分析 UFS術語縮略詞1 ATF的下載鏈接2 ATF BL1 ufshc_dme_get/set流程3 ufs總體架構圖3.1 UFS Top Level Architecture3.2 UFS System Model 4 ufshc_dme_get/set函數接口詳細分析4.1 ufshc_dme_get4.2 ufshc_dme_set4.3 ufshc_send_uic_cmd4.…

nodejs+vue+elementui考研互助交流網站

語言 node.js 框架&#xff1a;Express 前端:Vue.js 數據庫&#xff1a;mysql 數據庫工具&#xff1a;Navicat 開發軟件&#xff1a;VScode 前端nodejsvueelementui,該系統采用vue技術和B/S結構進行開發設計&#xff0c;后臺使用MySQL數據庫進行數據存儲。系統主要分為兩大模…

大數據課程J2——Scala的基礎語法和函數

文章作者郵箱&#xff1a;yugongshiyesina.cn 地址&#xff1a;廣東惠州 ▲ 本章節目的 ? 掌握Scala的基礎語法&#xff1b; ? 掌握Scala的函數庫&#xff1b; 一、Scala 基礎語法一 1. 概述 語句 說明 示例 var 用來聲明一個變量&#xff0c; 變量聲明后…

java面試題(16):Mysql一致性視圖是啥時候建立的

1 演示錯誤案例 先給大家來一個錯誤演示。 我們打開兩個會話窗口&#xff0c;默認情況下隔離級別是可重復讀&#xff0c;我們來看下&#xff1a; 首先在 A 會話中查看當前 user 表&#xff0c;查看完成后開啟事務&#xff1a; 可以看到id3的數據sex是男。 接下來在 B 會話中…

K8S系列一:概念入門

寫在前面 本文組織方式&#xff1a; K8S的架構、作用和目的。需要首先對K8S整體有所了解。 K8S是什么&#xff1f; 為什么是K8S&#xff1f; K8S怎么做&#xff1f; K8S的重要概念&#xff0c;即K8S的API對象。要學習和使用K8S必須知道和掌握的幾個對象。 Pod 實例 Volume 數…

php錯誤類型與處理

1 語法編譯錯誤&#xff0c;少了分號&#xff0c;這是系統觸發的錯誤&#xff0c;不需要我們去管。 2 錯誤類型有四種&#xff1a;error致命錯誤&#xff0c;代碼不會往下運行&#xff1b;warning&#xff1a;提醒錯誤&#xff0c;會往下運行&#xff0c;但是會有意想不到的結果…

【C++學習】STL容器——stack和queue

目錄 一、stack的介紹和使用 1.1 stack的介紹 1.2 stack的使用 1.3 stack的模擬實現 二、queue的介紹和使用 2.1 queue的介紹 2.2 queue的使用 2.3 queue的模擬實現 三、priority_queue的介紹和使用 3.1 priority_queue的介紹和使用 3.2 priority_queue的使用 3.4 p…

JVM---理解jvm之對象已死怎么判斷?

目錄 引用計數算法 什么是引用 可達性分析算法&#xff08;用的最多的&#xff09; 引用計數算法 定義&#xff1a;在對象中添加一個引用計數器&#xff0c;每當有一個地方引用它時&#xff0c;計數器值就加一&#xff1b;當引用失效時&#xff0c;計數器值就減一&#xff1…

國內外醫療器械政策法規網站集合

隨著醫療技術的不斷發展&#xff0c;醫療器械在現代醫療中扮演著重要的角色。為了確保醫療器械的安全性、有效性和質量&#xff0c;各國紛紛制定了一系列的政策法規來監管醫療器械的研發、生產、銷售和使用。這些政策法規的制定和實施對于保障公眾健康、促進醫療器械產業的健康…

docker--------介紹、常用命令,國內源配置

1 docker 國內源配置 # 鏡像&#xff1a;一堆文件 -目前從遠程倉庫下載的&#xff1a;https://hub.docker.com/ -鏡像有很多人提供&#xff1a;官方提供&#xff0c;第三方提供 -鏡像--》更新--》Tag不同版本 -centos:latest 最新 -docker pull 能找到…