单击更改android按钮颜色而不更改边框颜色?

时间:2015-06-28 18:34:29

标签: java android xml eclipse

我是Android新手,我正在设置我的xml文件。有没有办法改变按钮的颜色,感觉"点击"单击按钮然后返回原始颜色而不更改边框颜色? 我正在使用eclipse。 谢谢

2 个答案:

答案 0 :(得分:3)

首先,您需要在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>

答案 1 :(得分:2)

AndroidGeek,

您可以在drawable文件夹下的.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。