按压时按下不同的样式。

时间:2022-02-11 20:25:15

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

当按下按钮时,是否有方法对按钮应用样式?

If I have a style in style.xml:

如果我有风格。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.xml in my layout?

那么我该如何引用按钮呢?xml在我的布局?

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

Thanks!

谢谢!

7 个解决方案

#1


6  

Romain Guy suggests it is not possible:

罗曼·盖伊建议这是不可能的:

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

选择器只适用于可绘图,而非文本外观。目前还没有实现这一目标的计划。

#2


4  

You can achieve this by using an XML button definition.

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

Create an XML file in your drawable folder as follows:

在可绘制文件夹中创建一个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等也应该是.PNG文件在您的drawable文件夹中)

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.

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

#3


2  

You can make use of Color State List Resource

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

Example from link:

链接的例子:

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

XML文件保存在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:

此布局XML将把颜色列表应用到视图中:

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

#4


1  

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

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

在可绘制目录中创建一个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

创建一个button_background。在您的drawable文件夹中的xml文件,并添加按钮按下/单击,集中和默认颜色。button_background最终代码。xml文件是这样的。res /可拉的/ 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.

源。

#5


0  

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

#6


0  

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:

对于Bold,我也没有尝试过,但是你可以在你的selector i.s中提到文本样式。o参考其中的一种风格:

android:textStyle="bold"

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

如果这也不起作用,可以在button的onClick()中执行。

#7


-3  

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

我还没试过,但你可以在选择器中加入android:id="button"。

#1


6  

Romain Guy suggests it is not possible:

罗曼·盖伊建议这是不可能的:

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

选择器只适用于可绘图,而非文本外观。目前还没有实现这一目标的计划。

#2


4  

You can achieve this by using an XML button definition.

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

Create an XML file in your drawable folder as follows:

在可绘制文件夹中创建一个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等也应该是.PNG文件在您的drawable文件夹中)

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.

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

#3


2  

You can make use of Color State List Resource

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

Example from link:

链接的例子:

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

XML文件保存在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:

此布局XML将把颜色列表应用到视图中:

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

#4


1  

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

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

在可绘制目录中创建一个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

创建一个button_background。在您的drawable文件夹中的xml文件,并添加按钮按下/单击,集中和默认颜色。button_background最终代码。xml文件是这样的。res /可拉的/ 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.

源。

#5


0  

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

#6


0  

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:

对于Bold,我也没有尝试过,但是你可以在你的selector i.s中提到文本样式。o参考其中的一种风格:

android:textStyle="bold"

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

如果这也不起作用,可以在button的onClick()中执行。

#7


-3  

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

我还没试过,但你可以在选择器中加入android:id="button"。