【问题标题】:Button color change when button pressed按下按钮时按钮颜色改变
【发布时间】:2017-03-06 20:04:34
【问题描述】:

我已经尝试了一个多小时来改变button 的颜色,当我点击按钮时。

这是我的“mybutton.xml”

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

    android:shape="rectangle">
    <gradient
        android:startColor="#c4161616"
        android:endColor="#c4161616"
        android:angle="270" />
    <corners
        android:radius="10dp" />
    <stroke
        android:width="5px"
        android:color="#00FF00" />
</shape>

现在按钮看起来像这样:

当按钮被按下时,它应该是这样的:

<gradient
    android:startColor="#c4161616"
    android:endColor="#c4565656"
    android:angle="270" />

我是 Android 和 XML 的初学者。你能告诉我怎么做吗?

【问题讨论】:

  • 您需要一个选择器来执行此操作。定义 onfocus 状态,然后将其作为按钮的背景。它会像魅力一样起作用。祝你好运

标签: java android xml button


【解决方案1】:

兄弟试试这个:-

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-13
    • 2018-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多