如何同时为Button应用形状和选择器?

Kha*_*aza 74 android button selector

我已经为按钮应用了一个形状,如:

<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle" >
    <gradient android:startColor="#DD000000" android:endColor="#DD2d2d2d"  android:angle="90"></gradient>
    <corners android:radius="15dip"></corners>

</shape>
Run Code Online (Sandbox Code Playgroud)

现在我想使用一个选择器,如:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/active"
      android:state_pressed="true" />
<item android:drawable="@drawable/passive"/>
Run Code Online (Sandbox Code Playgroud)

对于这个按钮也是如此.可能吗 ...???

Han*_*nry 176

用这种方式:

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

   <item android:state_pressed="true" >
       <shape>.......</shape>
   </item>
   ..........
   ..........
</selector>
Run Code Online (Sandbox Code Playgroud)

  • 是的,它通过使用:<selector xmlns:android ="http://schemas.android.com/apk/res/android"> <item android:state_pressed ="true"> <shape android:shape ="rectangle" > <corner android:radius ="15dip"> </ corners> <solid android:color ="#febd26"/> </ shape> </ item> <item> <shape android:shape ="rectangle"> <gradient android:startColor ="#DD000000"android:endColor ="#DD2d2d2d"android:angle ="90"> </ gradient> <corner android:radius ="15dip"> </ corners> </ shape> </ item> </选择> (8认同)
  • @KhawarRaza将来,请在_question_中添加这样的信息,在那里可以格式化并更容易找到.随着时间的推移,评论往往会消失. (5认同)

AZ_*_*AZ_ 25

详细点回答

在中创建颜色资源

RES /值/ colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>


    <item name="yellow" type="color">#F7B500</item>
    <item name="yellow_dark" type="color">#AC7E00</item>

    <integer-array name="androidcolors">
        <item>@color/yellow</item>
        <item>@color/yellow_dark</item>
    </integer-array>

</resources>
Run Code Online (Sandbox Code Playgroud)

创建一个drawable at

RES /抽拉/ bg_yellow_round.xml

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

    <solid android:color="@color/yellow" />

    <stroke
        android:width="2dp"
        android:color="@color/yellow" />

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

</shape>
Run Code Online (Sandbox Code Playgroud)

创建另一个drawable,你想在同一个地方过渡并命名它

RES /抽拉/ bg_yellow_dark_round.xml

<solid android:color="@color/yellow_dark" />

<stroke
    android:width="2dp"
    android:color="@color/yellow_dark" />

<corners
    android:bottomLeftRadius="20dp"
    android:bottomRightRadius="20dp"
    android:topLeftRadius="20dp"
    android:topRightRadius="20dp" />
Run Code Online (Sandbox Code Playgroud)

现在创建一个颜色状态列表

RES /颜色/ btn_selector_yellow.xml

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

    <item android:drawable="@color/yellow" android:state_focused="true" android:state_pressed="false"/>
    <item android:drawable="@drawable/bg_yellow_dark_round" android:state_pressed="true"/>
    <item android:drawable="@drawable/bg_yellow_round"/>

</selector>
Run Code Online (Sandbox Code Playgroud)

现在将其设置为您的按钮,如下所示

<Button
                android:id="@+id/button1"
                android:layout_width="248dp"
                android:layout_height="44dp"
                android:layout_gravity="center_horizontal"
                android:layout_marginBottom="10dp"
                android:layout_marginTop="20dp"
                android:background="@color/btn_selector_yellow"
                android:text="AZ_ is so cool" />
Run Code Online (Sandbox Code Playgroud)

现在,这个会做过渡,从 淡黄色

暗黄色.


Ren*_*ira 10

shape.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/star_off"/>
    <corners android:radius="5dp"/>
    <padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" />
</shape>
Run Code Online (Sandbox Code Playgroud)

selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android" android:exitFadeDuration="@android:integer/config_mediumAnimTime">

    <item android:drawable="@color/tab_focused" android:state_focused="true" android:state_pressed="false"/>
    <item android:drawable="@color/tab_pressed" android:state_pressed="true"/>
    <item android:drawable="@drawable/shape"/>

</selector>
Run Code Online (Sandbox Code Playgroud)


Til*_*ill 8

您还可以创建一个使用内部选择器的形状。如果您的形状只是在不同状态下改变颜色,那么这会干净得多。

颜色/color_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/blue_dark" android:state_pressed="true" />
    <item android:color="@color/blue_light" />
</selector>
Run Code Online (Sandbox Code Playgroud)

可绘制/shape.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/color_selector" />
    <corners android:bottomLeftRadius="6dip" android:bottomRightRadius="6dp" />
    <padding android:bottom="0dip" android:left="0dip" android:right="0dip" android:top="0dip" />
</shape>
Run Code Online (Sandbox Code Playgroud)

  • 我知道这已经很旧了,但仅供记录,此方法仅适用于 Android 5.0 及更高版本。 (3认同)