AndroidButton

一、AndroidButton屬性

AndroidButton 是繼承自Button的控制項,所以Button的大部分屬性 AndroidButton 都會繼承,比如android:layout_width 和 android:layout_height,而 AndroidButton 獨有並且比較重要的屬性有:

1. app:cornerRadius

2. app:colorNormal

3. app:colorPressed

4. app:rippleColor

這些屬性也是 AndroidButton 的特色之處,下面我們將會對它們進行詳解。

二、AndroidButton按鈕背景顏色

AndroidButton 允許你通過配置按鈕背景顏色來使按鈕更具特色。這些顏色可以由 app:colorNormal 和 app:colorPressed 來指定。colorNormal 是指未按下狀態下的顏色,colorPressed 是指在按鈕被按下時的背景顏色。

下面是一個例子:

    <com.agrawalsuneet.squareloaderspack.loaders.WheelLoader
            android:id="@+id/squareLoader"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:colorNormal="#03A9F4"
            app:colorPressed="#0288D1" />

例子指定了一個叫做 squareLoader 的 AndroidButton,它的 colorNormal 是 #03A9F4,colorPressed 是 #0288D1。

三、AndroidButton居中

要讓 AndroidButton 水平居中可以通過在布局文件中設置 layout_gravity 屬性實現:

    <androidx.appcompat.widget.AppCompatButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Click me"
            android:layout_gravity="center_horizontal"/>

上述代碼中的 layout_gravity 屬性設置為 center_horizontal,它可以使 button 在水平方向上自動居中。

四、AndroidButton背景顏色

對於 AndroidButton,我們還可以通過 app:rippleColor 和 app:cornerRadius 來對按鈕進行更多的樣式自定義。

app:rippleColor 是指按鈕點擊時的背景顏色。app:cornerRadius 是指 AndroidButton 的圓角大小。下面是一個代碼示例:

    <com.agrawalsuneet.squareloaderspack.loaders.WheelLoader
            android:id="@+id/squareLoader"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:colorNormal="#03A9F4"
            app:colorPressed="#0288D1"
            app:cornerRadius="5dp"
            app:rippleColor="#FF4081" />

上述代碼中的cornerRadius屬性設置為 5dp,它將會使 AndroidButton 的圓角大小為 5dp。rippleColor 屬性設置為 #FF4081,它將會在按鈕點擊時彈出波紋的深粉色顏色。

五、AndroidButton圓角代碼

app:cornerRadius 屬性可以用來設置 AndroidButton 的圓角大小。我們可以把圓角大小設為大於 0 的任意值。下面是一個 AndroidButton 圓角代碼的示例:

    <com.agrawalsuneet.squareloaderspack.loaders.WheelLoader
            android:id="@+id/squareLoader"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:colorNormal="#03A9F4"
            app:colorPressed="#0288D1"
            app:cornerRadius="10dp"
            app:rippleColor="#FF4081" />

上述代碼中的 cornerRadius 屬性設置為 10dp,它將會使 AndroidButton 的圓角大小為 10dp。

六、AndroidButton顏色怎麼改

AndroidButton 的顏色可以使用 app:colorNormal 和 app:colorPressed 屬性進行修改。顏色值為 16 進位顏色代碼。

下面是一個代碼示例:

    <com.agrawalsuneet.squareloaderspack.loaders.WheelLoader
            android:id="@+id/squareLoader"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:colorNormal="#4CAF50"
            app:colorPressed="#388E3C"
            app:cornerRadius="5dp"
            app:rippleColor="#FF4081" />

上述代碼中的 colorNormal 屬性設置為 #4CAF50,它將會使未按下狀態下 AndroidButton 的背景色為綠色。colorPressed 屬性設置為 #388E3C,它將會使按下狀態下 AndroidButton 的背景色為綠色。

七、AndroidButton點擊前後顏色

AndroidButton 點擊前後顏色的代碼實現和顏色的修改相似,只需要設置 app:colorNormal 和 app:colorPressed 屬性即可。

下面是一個代碼示例:

    <com.agrawalsuneet.squareloaderspack.loaders.WheelLoader
            android:id="@+id/squareLoader"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:colorNormal="#F44336"
            app:colorPressed="#E53935"
            app:cornerRadius="5dp"
            app:rippleColor="#FF4081" />

上述代碼中的 colorNormal 屬性設置為 #F44336,它將會使未按下狀態下 AndroidButton 的背景色為紅色。colorPressed 屬性設置為 #E53935,它將會使按下狀態下 AndroidButton 的背景色為紅色。

八、AndroidButton如何將兩個按鈕並排

在 Android 中,我們可以使用LinearLayout 或者 RelativeLayout 實現並排布局。下面是一個代碼示例:

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal">

        <Button
            android:id="@+id/button1"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:text="Button 1"/>

        <Button
            android:id="@+id/button2"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:text="Button 2"/>
            
    </LinearLayout>

上述代碼中將兩個按鈕都放到了一個 LinearLayout 中,並將 LinearLayout 的 orientation 屬性設置為 horizontal。當前布局將在水平方向上對其兩個按鈕,使它們位於同一行。

九、android按鈕樣式

AndroidButton 的樣式可以使用 android:background 屬性進行設置。下面是一個代碼示例:

    <Button
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="Sign up"
        android:background="@drawable/button_background_yellow"
        android:textColor="@color/colorWhite"/>

上述代碼中給AndroidButton設置了一個自定義背景圖。並將 AndroidButton 的文字顏色設置為白色。可以根據自己的需求去調整這些屬性,自定義 AndroidButton 的樣式。

十、android studio按鈕控制項選取

Android Studio 是一款非常好用的 Android 開發工具,它內置了大量的工具和代碼資源,包括各種控制項,比如 AndroidButton。您可以通過在您的項目中使用控制項庫,直接從項目中拖動 AndroidButton 至您的布局文件中。

下面是一個代碼示例:

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal">

        <Button
            android:id="@+id/button1"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:text="Button 1"/>

        <Button
            android:id="@+id/button2"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:text="Button 2"/>
            
    </LinearLayout>

上述代碼中將兩個按鈕都放到了一個 LinearLayout 中,並將 LinearLayout 的 orientation 屬性設置為 horizontal。當前布局將在水平方向上對其兩個按鈕,使它們位於同一行。

通過這樣的操作,您可以輕鬆在 Android Studio 中添加 AndroidButton。

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/181392.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-11-23 06:40
下一篇 2024-11-23 06:40

發表回復

登錄後才能評論