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

时间:2011-09-30 07:02:08

标签: 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>

现在我想使用像:

这样的选择器
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/active"
      android:state_pressed="true" />
<item android:drawable="@drawable/passive"/>

这个按钮也是如此。有可能...... ???

9 个答案:

答案 0 :(得分:167)

使用这种方式:

<?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>

答案 1 :(得分: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>

创建一个drawable

<强> 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>

创建另一个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" />

现在在

创建一个color state list

<强> 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>

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

<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" />

现在,这将从 light yellow

转换

dark yellow

答案 2 :(得分: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>

<强> 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>

答案 3 :(得分:3)

您还可以创建使用内部选择器的形状。如果你的形状只是在不同的状态下改变颜色,那就更清洁了。

<强>颜色/ 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>

<强>抽拉/ 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>

答案 4 :(得分:3)

这是我的方式,它的确有效!

 $.ajax({
  method: "POST",
   url: "q&a.php",
  data: { name: "John", location: "Boston" } //data which you want to send as form data
})
  .done(function( msg ) { //callback function
    alert( "Data Saved: " + msg );
  });

答案 5 :(得分:1)

我的示例是带有state_pressed的圆形按钮。 代码如下:

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


    <item android:state_pressed="true">
        <shape android:shape="oval">
            <solid android:color="@color/light_primary_color" />
        </shape>

    </item>
    <item>
        <shape android:shape="oval">
            <solid android:color="@color/accent_color" />
        </shape>
    </item>

</selector>

答案 6 :(得分:1)

嗯,我知道这太晚了 但这是一个解决的例子

 <TextView
            android:id="@+id/txt_out_going_calls"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="04dp"
            android:layout_weight="1"
            android:background="@drawable/header_text_view_selector"
            android:gravity="center"
            android:text="@string/outgoing_calls_tab_button_text"
            android:textColor="@color/home_text_color" />

和我的header_text_view_selector

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

        <shape xmlns:android="http://schemas.android.com/apk/res/android">
            <solid android:color="@color/home_fragment_tab_color_selected"/>
            <corners android:radius="25dip" />
            <padding android:bottom="08dip" android:left="9dip" android:right="9dip" android:top="08dip" />
        </shape>
    </item>
    <item android:state_selected="false">

        <shape>
            <solid android:color="@color/home_fragment_tab_color_simple"/>
            <corners android:radius="25dip" />
            <padding android:bottom="08dip" android:left="9dip" android:right="9dip" android:top="08dip" />
        </shape>
    </item>
</selector>

所以基本上我用选择器创建一个舍入的textview。这里我只处理state_selectednot_selected。希望它有所帮助

答案 7 :(得分:1)

为了更加可重用,您可以在单个属性上设置状态。 避免重复形状

<selector
    xmlns:android="http://schemas.android.com/apk/res/android"
>
    <item>
        <shape android:shape="rectangle" >
            <corners android:radius="5dp"/>
            <solid
                android:state_enabled="false"
                android:color="@color/transparent"
            />
            <solid
                android:state_enabled="true"
                android:color="@color/background"
            />
            <stroke
                android:width="@dimen/dividerHeight"
                android:color="@color/dividerLight"
            />
        </shape>
    </item>
</selector>

我可以使用此方法禁用后以编程方式设置背景。

答案 8 :(得分:0)

在使用任何图像时使用形状名称,并在使用图像时使用它。试试你不会遇到任何问题。这就是你问的问题吗?