【问题标题】:Button highlight in androidandroid中的按钮突出显示
【发布时间】:2013-03-26 05:10:30
【问题描述】:

我的应用程序布局文件中有以下按钮代码

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

默认情况下,按下按钮时,其突出显示颜色为蓝色。如何将其更改为我想要的颜色?(我希望它是深红色的)

【问题讨论】:

标签: android button colors


【解决方案1】:

一个按钮有一个set of states can be configured 像这样:

<?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/drawables 文件夹中将其创建为文件,然后在按钮中将其用作背景。假设您将该文件命名为“my_button.xml”,您可以像这样使用它:

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

或者像这样:

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

您的颜色可以在 res/values 文件夹中的 colors.xml 中定义。如果您没有此文件,您可以创建它(android 会识别它)。这是一个很好的做法,但您也可以在上面的代码中将 your_color 替换为 #DC143C。

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

请注意,此颜色已为 crimson 设置。

您还可以为背景添加图像,将“@color/your_color”替换为“@drawable/your_image”。

有关更多信息,您可以在 stackoverflow 中关注this link

【讨论】:

  • 我似乎在 res/values 中找不到 colors.xml 文件...我只能找到 styles.xml 和 strings.xml 文件
  • 你必须创建它。这是一个很好的做法,因为您有一个包含所有所需颜色的文件。您也可以直接将 your_color 替换为 #DC143C。文件创建不是强制性的。只是一个好的做法。
  • 作为旁注,您还可以创建一个dimens.xml 来保存您的尺寸值(例如,当您设置宽度或高度时)。 @dimen/my_dimen 可以访问这些值。
【解决方案2】:

在您的可绘制文件夹中创建以下 xml 文件。并将您的按钮背景设置为此可绘制对象。

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

        <!-- Image display in background in select state -->
        <item android:drawable="@drawable/button_pressed" android:state_pressed="true"/>

        <!-- Image display in background in select state -->
        <item android:drawable="@drawable/button_pressed" android:state_focused="true"/>

        <!-- Default state -->
        <item android:drawable="@drawable/button"/>

    </selector>

【讨论】:

    【解决方案3】:

    您需要的是选择器,它是一个简单的可绘制文件,您可以在其中根据按钮的状态更改按钮的颜色,例如:

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

    【讨论】:

      【解决方案4】:

      为了让您找到正确的资源,这里是 StateList Drawable 的文档。
      check this link

      您只需在 res/drawable 子目录中创建和定义它,并将其设置为按钮背景。

      【讨论】:

        【解决方案5】:

        您可以像这样在所有控件上定义颜色

        <style name="AppTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
                <!-- Some attributes like windowNoTitle, windowActionBar, ... -->
                <item name="colorControlHighlight">@color/colorAccent</item> <!-- chose the color here -->
        </style>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-09-27
          • 2011-03-05
          • 2019-04-27
          • 1970-01-01
          • 2020-08-22
          • 2013-05-24
          相关资源
          最近更新 更多