溫馨提示×

溫馨提示×

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

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android button, xml文件定義形狀,代碼中修改背景顏色

發布時間:2020-06-09 15:16:27 來源:網絡 閱讀:1381 作者:wehappy168 欄目:移動開發

1. 首先在drawable文件夾定義一個shape.xml文件,內容如下:

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

    <corners
        android:topLeftRadius="10dp"
        android:topRightRadius="10dp"
        android:bottomRightRadius="10dp"
        android:bottomLeftRadius="10dp" />

    <padding
        android:left="30dp"
        android:top="0dp"
        android:right="30dp"
        android:bottom="0dp"
        />
</shape>


2. 在main.xml文件中,button使用這個shape.xml,如下:

<Button
        android:id="@+id/button_next"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:text="@string/button_next"
        android:textSize="@dimen/label_text_size"
        android:layout_alignParentBottom="true"
        android:layout_centerVertical="true"
        android:gravity="center_horizontal|center_vertical"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:background="@drawable/shape"
        />

這時,該button就顯示如shape.xml定義的形狀。


3. 在代碼中動態修改button 背景顏色,代碼如下:

GradientDrawable bgShape = (GradientDrawable)buttonNext.getBackground();
bgShape.setColor(Color.BLUE);


向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女