簡體   English   中英

在不更改邊框顏色的情況下更改單擊時的an​​droid按鈕顏色?

[英]Change android button colour on click without changing border colour?

我是Android新手,正在設置xml文件。 有沒有一種方法可以在單擊按鈕時以一種“單擊”的感覺來更改按鈕的顏色,然后又返回到其原始顏色,而無需更改邊框的顏色? 我正在使用日食。 謝謝

首先,您需要在Drawable文件夾中制作一些xml文件:

btn_clicked.xml:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
   android:shape="rectangle">
   <corners android:radius="20dp" />
   <solid android:color="#ff0000" />
   <stroke
       android:width="1dp"
       android:color="#fff" />
</shape>

btn_normal.xml:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
   <corners android:radius="20dp" />
   <solid android:color="#00ff00" />
   <stroke
      android:width="1dp"
      android:color="#fff" />
</shape>

name_btn.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:drawable="@drawable/btn_clicked" android:state_pressed="true"/>
   <item android:drawable="@drawable/btn_normal"/>
</selector>

然后像這樣使用它:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent" android:layout_height="match_parent">
        <Button android:layout_width="140dp" android:layout_height="40dp"
            android:background="@drawable/cancel_btn" />
</RelativeLayout>

AndroidGeek,

您可以在可繪制文件夾下的.XML文件中指定效果,然后將文件鏈接到布局文件夾下的Button。

例:

在您的activity_main.xml內部,您的Button標記將如下所示:

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Styled Button"
        android:id="@+id/styledButton"
        android:background="@drawable/button"/>

在res / drawable文件夾下創建一個XML文件[示例:button.xml],以指定按下按鈕時將發生的更改。

以下是button.xml的內容

<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">


    <!-- Changes that the button has to show when it is pressed -->

    <item android:state_pressed="true" >
        <shape>
            <solid
                android:color="#ef4444" />

            <!-- Keep the stroke values same if you want the same border effect, irrespective you press / un-press the button
            In case ih you want to change the border thickness, while the button is being clicked,
             then increase the "android:width" value -->
            <stroke
                android:width="1dp"
                android:color="#992f2f" />
            <corners
                android:radius="6dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>



    <!-- Default appearance when the button is displayed  -->
    <item>
        <shape>
            <gradient
                android:startColor="#ef4444"
                android:endColor="#992f2f"
                android:angle="270" />
            <stroke
                android:width="1dp"
                android:color="#992f2f" />
            <corners
                android:radius="6dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
</selector>

希望這可以幫助。 不管IDE如何,它都應該起作用。 Eclipse和Android Studio。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM