在选定状态下更改按钮形状?

时间:2015-09-15 09:30:38

标签: java android android-studio android-button

我有一个Android按钮:

<Button
       android:layout_width="wrap_content"
       android:layout_height="44dp"
       android:background="@drawable/action_button" />

action_button.xml是:

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

    <padding
        android:left="25dp"
        android:right="25dp"/>

    <gradient
        android:angle="0"
        android:endColor="@color/whiteColor"
        android:startColor="@color/whiteColor"/>

    <corners android:radius="@dimen/borderRadius"/>

    <stroke
        android:width="2px"
        android:color="@color/actionColor"/>
</shape>

按下按钮后如何更改形状?

3 个答案:

答案 0 :(得分:2)

您好请使用以下选择器并根据您的需要进行更改

selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
   <!-- <item android:state_enabled="false"
          android:drawable="@drawable/cyan_strip" />
    <item android:state_pressed="true"
          android:drawable="@drawable/cyan_strip" />
    <item android:state_pressed="false" android:state_focused="true"
          android:drawable="@drawable/cyan_strip" />
    <item android:drawable="@drawable/black_strip" /> -->

    <item android:state_enabled="false">
        <shape>
        <solid
            android:color="#4ec9d9" />
        <stroke
            android:width="1dp"
            android:color="#FFFFFF" />
        <padding
            android:left="1dp"
            android:top="1dp"
            android:right="1dp"
            android:bottom="1dp" />
    </shape>
    </item>

    <item android:state_pressed="true">
        <shape>
        <solid
            android:color="#4ec9d9" />
        <stroke
            android:width="1dp"
            android:color="#FFFFFF" />
        <padding
            android:left="1dp"
            android:top="1dp"
            android:right="1dp"
            android:bottom="1dp" />
    </shape>
    </item>

    <item android:state_pressed="false" android:state_focused="true">
        <shape>
        <solid
            android:color="#4ec9d9" />
        <stroke
            android:width="1dp"
            android:color="#FFFFFF" />
        <padding
            android:left="1dp"
            android:top="1dp"
            android:right="1dp"
            android:bottom="1dp" />
    </shape>
    </item>

    <item android:state_selected="true">
        <shape>
        <solid
            android:color="#4ec9d9" />
        <stroke
            android:width="1dp"
            android:color="#FFFFFF" />
        <padding
            android:left="1dp"
            android:top="1dp"
            android:right="1dp"
            android:bottom="1dp" />
    </shape>
    </item>

    <item >
       <shape>
        <solid
            android:color="#000000" />
        <stroke
            android:width="1dp"
            android:color="#FFFFFF" />
        <padding
            android:left="1dp"
            android:top="1dp"
            android:right="1dp"
            android:bottom="1dp" />
    </shape> 
    </item>

</selector>

答案 1 :(得分:1)

你需要一个选择器

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/action_button_pressed" />
    <item android:drawable="@drawable/action_button" />
</selector>

将其放在xml下的drawable/文件中,并将其用作Button的背景。 Android将负责其余的

答案 2 :(得分:0)

见: Link

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:startColor="#302226"
        android:endColor="#6D7B8D"
        android:angle="45"/>
    <padding android:left="6dp"
        android:top="6dp"
        android:right="6dp"
        android:bottom="6dp" />
    <corners android:radius="30dp" />

</shape>

或看:

 <ImageButton
                 android:id="@+id/imageButton1"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:src="@drawable/shape_oval" />

Shape_oval是一个我们可以从谷歌搜索或绘画自己在油漆或photoshop中拍摄的图像。