Android 按下时对按钮应用不同的样式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3730217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:33:28  来源:igfitidea点击:

Apply Different Style to Button When Pressed

androidbuttoncoding-stylepressed

提问by user440308

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

有没有办法在按下按钮时将样式应用于按钮?

If I have a style in style.xml:

如果我在style.xml 中有一个样式:

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

a selector in button.xml:

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.xmlin my layout?

那么我将如何在我的布局中引用button.xml呢?

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

Thanks!

谢谢!

回答by esilver

Romain Guy suggests it is not possible:

Romain Guy 表示这是不可能的:

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

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

“选择器仅适用于可绘制对象,不适用于文本外观。目前没有计划实现这一目标。”

回答by rhinds

You can achieve this by using an XML button definition.

您可以通过使用 XML 按钮定义来实现这一点。

Create an XML file in your drawable folder as follows:

在 drawable 文件夹中创建一个 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="@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)

如您所见,这允许您定义不同的按钮图像以用于不同的状态(black_button、green_button 等也应该是 drawable 文件夹中的 .PNG 文件)

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

现在,从您的 layout.xml 文件中,您可以将按钮背景设置为指向按钮选择器:

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

然后可以像任何图像文件一样从可绘制文件夹中引用选择器 XML。

回答by Bartosz W?gielewski

You can make use of Color State List Resource

您可以使用颜色状态列表资源

Example from link:

来自链接的示例:

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

保存在 res/color/button_text.xml 中的 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:

此布局 XML 将颜色列表应用于视图:

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

回答by Habib BenAlaya

To Change Android Button on Click/Press Color :

要在单击/按下颜色时更改 Android 按钮:

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

要定义颜色值,您必须在项目值目录中创建 colors.xml 文件并添加以下内容。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

在 Drawable 目录中创建一个 XML 文件

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

在 drawable 文件夹中创建一个 button_background.xml 文件并添加按钮按下/点击、聚焦和默认颜色。button_background.xml 文件的最终代码如下所示。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

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.

来源在这里

回答by Alex Volovoy

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

回答by mdv

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:

对于按下时的粗体,我也没有尝试过,但也许您可以在选择器中提及文本样式,iso 指的是它的样式:

android:textStyle="bold"

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

如果这也不起作用,您可以在按钮的 onClick() 中进行。

回答by FixerMark

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

我还没有尝试过,但想必您可以将其包含android:id="button"在您的选择器中。