android studio實現圓角的button


操作過程

1. 在drawable中新建一個button_circle_shape.xml

但是建立這個xml是有操作的,因為從drawable右鍵是創建不了xml的, 具體操作如下:

右鍵res–>New–>Android resourse file

在這里插入圖片描述

注意Root element 是不能選擇的 是直接填寫為shape, 默認應該是selector 把它改為shape即可

在這里插入圖片描述

然后就可以看見在drawable 成功添加了xml
在這里插入圖片描述

2.填寫代碼

button_circle_shape.xml 內容如下
這個xml里有一個shape,此shape規定了圓角按鈕的樣式

<?xml version="1.0" encoding="UTF-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <!-- 填充的顏色 -->
    <solid android:color="#FFFFFF" />
    <!-- android:radius 弧形的半徑 -->
    <!-- 設置按鈕的四個角為弧形 -->
    <corners 
    android:radius="5dip" />  
    <!--也可單獨設置-->
    <!-- <corners -->
   <!-- android:topLeftRadius="10dp"-->
   <!-- android:topRightRadius="10dp"-->
   <!-- android:bottomRightRadius="10dp"-->
  <!--  android:bottomLeftRadius="10dp"-->
 <!--   />  -->
        **設置文字padding**
    <!-- padding:Button里面的文字與Button邊界的間隔 -->
    <padding
        android:left="10dp"
        android:top="10dp"
        android:right="10dp"
        android:bottom="10dp"
        />
</shape>

3.使用shape:

給button的background屬性賦值為剛創建的

android:background="@drawable/button_circle_shape"


免責聲明!

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



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