亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android 圓角按鈕的實現(xiàn)代碼

 更新時間:2023年12月19日 11:00:02   作者:tracydragonlxy  
這篇文章主要介紹了Android 圓角按鈕的實現(xiàn)代碼,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧

Android 圓角按鈕的實現(xiàn)

效果圖:

1.在res/drawable目錄下新建按鈕樣式文件 btn_normal.xml(正常狀態(tài)) 和 btn_pressed.xml(按下狀態(tài))。

btn_normal.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <!-- 圓角的半徑 -->
    <corners android:radius="10dp"/>
    <!-- 填充顏色 -->
    <solid android:color="#3a8fea"/>
</shape>

btn_pressed.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<shape 
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:shape="rectangle">
    <!-- 圓角的半徑 -->
    <corners android:radius="10dp"/>
    <!-- 填充顏色 -->
    <solid android:color="#0662f5"/>
</shape>

2.在res/drawable目錄下新建樣式文件 btn_selector.xml 文件,定義按鈕的不同狀態(tài)樣式。

btn_selector.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 正常狀態(tài) -->
    <item android:drawable="@drawable/btn_normal" android:state_pressed="false"/>
    <!-- 按下狀態(tài) -->
    <item android:drawable="@drawable/btn_pressed" android:state_pressed="true"/>
</selector>

3.使用按鈕樣式。

activity_button.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ButtonActivity">
    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:layout_marginStart="30dp"
        android:text="button"
        android:textColor="#fff"
        android:background="@drawable/btn_selector"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

android:background=“@drawable/btn_selector
其中的btn_selector是我們自定義的xml樣式文件。

運行結(jié)果圖:

4.給圓角按鈕加上虛線邊框樣式。

btn_normal.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <!-- 圓角的半徑 -->
    <corners android:radius="10dp"/>
    <!-- 填充顏色 -->
    <solid android:color="#3a8fea"/>
    <!-- 邊框的寬度,每段虛線的長度,和兩段虛線之間的間隔和顏色 -->
    <stroke
        android:width="2dp"
        android:dashWidth="6dp"
        android:dashGap="6dp"
        android:color="#e75050" />
</shape>

運行結(jié)果圖:

在這里插入圖片描述

如果希望按鈕邊框是實線,那么把dashWidth和dashGap屬性去除即可。

實線邊框樣式:

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <!-- 圓角的半徑 -->
    <corners android:radius="10dp"/>
    <!-- 填充顏色 -->
    <solid android:color="#3a8fea"/>
    <!-- 實線邊框 -->
    <stroke
        android:width="2dp"
        android:color="#e75050" />
</shape>

運行結(jié)果圖:

在這里插入圖片描述

5.實現(xiàn)局部圓角樣式。

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <!-- 圓角的半徑,左上/右下實現(xiàn)圓角 -->
    <corners
        android:topLeftRadius="10dp"
        android:bottomRightRadius="10dp"/>
    <!-- 填充顏色 -->
    <solid android:color="#3a8fea"/>
</shape>

運行結(jié)果圖:

6.漸變背景色樣式。

<gradient
  android:angle="integer"
  android:centerX="Float"
  android:centerY="Float"
  android:centerColor="integer"
  android:startColor="color"
  android:endColor="color"
  android:gradientRadius="integer"
  android:type=["linear"|"radial"|"sweep"]
  android:usesLevel=["true"|"false"]
  />

angle:角度,當 android:type=“linear”時有效 ,以45度為單位,逆時針方向旋轉(zhuǎn)
centerX:Float。漸變色中心的 X 相對位置( 0-1.0 )。當 android:type=“linear”時無效
centerY:Float。漸變色中心的 Y 相對位置( 0-1.0 )。當 android:type=“linear”時無效
centerColor:color??蛇x的顏色,出現(xiàn)在 start 和 end 顏色之間。
gradientRadius:Float。漸變色的半徑。當 android:type=“radial” 時有效。
startcolor:開始的顏色
endcolor:結(jié)束的顏色
type:漸變色的樣式。有效值為:
“linear”:線性漸變,默認值
“radial”:環(huán)形漸變。 start 顏色是處于中間的顏色
“sweep”:扇形漸變
useLevel:Boolean。“ true ”表示可以當作 LevelListDrawable 使用

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <!-- 圓角的半徑 -->
    <corners android:radius="10dp"/>
    <!-- 填充顏色 -->
    <solid android:color="#3a8fea"/>
    <gradient
        android:angle="180"
        android:startColor="#f00"
        android:centerColor="#0f0"
        android:endColor="#00f"
        android:type="linear"
        android:useLevel="true"/>
</shape>

運行結(jié)果圖:

到此這篇關(guān)于Android 圓角按鈕的實現(xiàn)的文章就介紹到這了,更多相關(guān)android 圓角按鈕內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論