在Android的按钮高亮按钮、Android、高亮

2023-09-05 04:47:28 作者:灼烧.

我有以下的code在布局文件中的按钮,我的应用程序

I have the following code for a button in the layout file for my application

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:text="Start updating" />

在默认情况下,当按钮是pressed其高光颜色是蓝色。我要如何改变这种状况,以我想要的颜色?(我想这是深红色)

By default when the button is pressed its highlight color is blue. How do I change that to the color I want?(i want it to be crimson)

推荐答案

一个按钮有一个的设定状态的可以配置是这样的:

A button has a set of states that can be configured like this:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/your_color"
      android:state_pressed="true" />
    <item android:drawable="@color/your_color"
      android:state_focused="true" />
</selector>

您可以创建本作中,你的RES /可绘制一个文件夹,然后用它在你的按钮为背景。 Supose你叫那个文件my_button.xml然后你可以使用它是这样的:

You can create this as a file in your res/drawables folder and then use it in your button as the background. Supose that you called that file "my_button.xml" you can then use it like this:

<Button
    android:background="@drawable/my_button"  

或者是这样的:

Or like this:

my_button.setBackgroundDrawable(getResources().getDrawable(R.drawable.my_button));

您的颜色可以在colors.xml被定义在res /值的文件夹。如果没有这个文件,你可以创建它(机器人会识别它)。这是一个很好的做法,但你也可以通过#DC143C在code以上替换your_color。

Your color can be defined in the colors.xml in the res/values folder. If you don't have this file you can create it (android will recognize it). This is a good practise, but you can also replace your_color by #DC143C in the code above.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="your_color">#DC143C</color>
</resources>

请注意,这个颜色已经为绯红设置。

Note that this color is already set for crimson.

您还可以添加图片为背景,通过@绘制/ your_image取代@色/ your_color。

You can also add an image for the background, replacing the "@color/your_color" by "@drawable/your_image".

有关更多信息,你可以按照this在计算器链接。

For more information you can follow this link in stackoverflow.