溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Android Button的邊框陰影

發(fā)布時(shí)間:2024-08-31 13:09:56 來(lái)源:億速云 閱讀:83 作者:小樊 欄目:移動(dòng)開(kāi)發(fā)

在 Android 中,為 Button 添加邊框和陰影可以通過(guò)創(chuàng)建一個(gè)自定義的背景 XML 文件來(lái)實(shí)現(xiàn)

  1. res/drawable 目錄下創(chuàng)建一個(gè)名為 button_border_shadow.xml 的新文件。如果 drawable 目錄不存在,請(qǐng)創(chuàng)建一個(gè)。

  2. 將以下代碼粘貼到 button_border_shadow.xml 文件中:

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
       <layer-list>
            <item>
               <shape android:shape="rectangle">
                    <solid android:color="#1E88E5" />
                    <corners android:radius="4dp" />
                   <padding
                        android:left="1dp"
                        android:top="1dp"
                        android:right="1dp"
                        android:bottom="2dp" />
                </shape>
            </item>
            <item>
               <shape android:shape="rectangle">
                    <solid android:color="@android:color/white" />
                    <corners android:radius="4dp" />
                   <stroke
                        android:width="1dp"
                        android:color="#1E88E5" />
                </shape>
            </item>
        </layer-list>
    </item>
</selector>

這段代碼創(chuàng)建了一個(gè)帶有藍(lán)色邊框和陰影的 Button。你可以根據(jù)需要修改顏色、圓角半徑和邊框?qū)挾取?/p>

  1. 在布局文件(例如 activity_main.xml)中,將 Button 的 android:background 屬性設(shè)置為 @drawable/button_border_shadow
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="點(diǎn)擊我"
    android:background="@drawable/button_border_shadow" />

現(xiàn)在,你的 Button 應(yīng)該具有邊框和陰影效果。你可以根據(jù)需要調(diào)整 button_border_shadow.xml 文件中的顏色和尺寸。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI