Apply Different Style to Button When Pressed

蓝咒 提交于 2019-12-21 09:07:56

问题


Is there a way to apply a style to a button when the button is pressed?

If I have a style in style.xml:

<resources>
    <style name="test">
        <item name="android:textStyle">bold</item>
    </style>
</resources>

a selector in button.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/test_pressed"
              style="@style/test"
          android:state_pressed="true"/>
    <item android:drawable="@drawable/test_focused"
          android:state_focused="true"/>
    <item android:drawable="@drawable/test_normal"/>
</selector>

then how would I reference button.xml in my layout?

<Button
        ...
        android:???="button"/>

Thanks!


回答1:


Romain Guy suggests it is not possible:

http://code.google.com/p/android/issues/detail?id=8941

"Selector works only for drawables, not text appearances. There is currently not plan to make this happen."




回答2:


You can achieve this by using an XML button definition.

Create an XML file in your drawable folder as follows:

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

        <!-- <item android:state_focused="true"
        android:drawable="@drawable/button_focused" /> --> 

        <item android:drawable="@drawable/black_button" />
</selector>

As you can see this allows you to define different button images to use for the different states (black_button, green_button etc should be .PNG files in your drawable folder also)

Now, from your layout.xml file you can just set the button background to point to the button selector:

<Button android:text="Play" android:id="@+id/playBtn"
            android:background="@drawable/button_selector"
            android:textColor="#ffffff" />

The Selector XML can then be referenced from teh drawable folder like any image file can.




回答3:


You can make use of Color State List Resource

Example from link:

XML file saved at res/color/button_text.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:color="#ffff0000"/> <!-- pressed -->
    <item android:state_focused="true"
          android:color="#ff0000ff"/> <!-- focused -->
    <item android:color="#ff000000"/> <!-- default -->
</selector>

This layout XML will apply the color list to a View:

<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/button_text"
    android:textColor="@color/button_text" />



回答4:


To Change Android Button on Click/Press Color :

Define Color Value

To define color value, you have to create colors.xml file in your project values directory and add following. res/values/colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="button_pressed">#ff8a00</color>
    <color name="button_focused">#ff8a00</color>
    <color name="button_default">#1c76bb</color>
</resources>

Create a XML File in Drawable Directory

Create a button_background.xml file in your drawable folder and add button pressed/clicked, focused and default color. Final code of button_background.xml file looks like this. res/drawable/button_background.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
        android:drawable="@color/button_pressed"/> <!-- pressed -->
    <item android:state_focused="true"
        android:drawable="@color/button_focused"/> <!-- focused -->
    <item android:drawable="@color/button_default"/> <!-- default -->
</selector>

Adding Buttons

res/activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/button_background"
        android:text="Click Me"
        android:textColor="#fff" />

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
        android:layout_marginTop="16dp"
        android:background="@drawable/button_background"
        android:text="Click Me"
        android:textColor="#fff" />

</RelativeLayout>

Source here.




回答5:


<Button
        android:background="@drawable/button"/>



回答6:


To refer to the selector, you must give it as the background for that Button.

<Button
     android:background="@drawable/button"
/> 

And for Bold when pressed, I haven't tried it either, but maybe you can mention text style in your selector, i.s.o referring to a style from it:

android:textStyle="bold"

If this also does not work, you can do it in button's onClick().




回答7:


I've not tried it but presumably you could just include android:id="button" in your selector.



来源:https://stackoverflow.com/questions/3730217/apply-different-style-to-button-when-pressed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!