【问题标题】:How to change color of button when clicked through XML only (without another drawable)?仅通过 XML(没有其他可绘制对象)单击时如何更改按钮的颜色?
【发布时间】:2014-02-15 08:43:03
【问题描述】:

我想改变按钮按下或聚焦时的颜色,或者实际上是任何状态。但是,我不仅想通过 XML 做到这一点,而且不使用另一个可绘制对象。解决此问题的先前问题,例如Highlight a button when it pressed without using two drawable?How to Change color of Button in Android when Clicked?,要么以编程方式不使用drawable,要么通过XML 使用drawable。是否有我可以在 button.xml 文件中设置的属性,或者可以在突出显示或单击时更改视图背景颜色的属性,而无需咨询 Java?

这是我的 activity_main.xml 的代码。如果还不是很明显,我正在使用游戏板的静态图像到电话井字游戏。

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

<ImageView
    android:id="@+id/gameBoard"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:contentDescription="@string/gameboard"
    android:src="@drawable/gameboard" />

<ImageButton
    android:id="@+id/squareOne"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_marginBottom="288dp"
    android:layout_marginRight="219dp"
    android:layout_marginTop="71dp"
    android:background="@android:color/transparent"
    android:contentDescription="@string/top_left"
    android:src="@drawable/blank_button" />

<ImageButton
    android:id="@+id/squareTwo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/squareOne"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@+id/squareOne"
    android:layout_marginLeft="110dp"
    android:layout_marginRight="108dp"
    android:background="@android:color/transparent"
    android:contentDescription="@string/top_middle"
    android:src="@drawable/blank_button" />

<ImageButton
    android:id="@+id/squareThree"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/squareTwo"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@+id/squareTwo"
    android:minWidth="100dp"
    android:background="@android:color/transparent"
    android:contentDescription="@string/top_right"
    android:src="@drawable/blank_button" />

<ImageButton
    android:id="@+id/squareFour"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignRight="@+id/squareOne"
    android:layout_below="@+id/squareTwo"
    android:layout_marginBottom="179dp"
    android:layout_marginTop="9dp"
    android:background="@android:color/transparent"
    android:contentDescription="@string/middle_left"
    android:src="@drawable/blank_button" />

<ImageButton
    android:id="@+id/squareFive"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/squareFour"
    android:layout_alignLeft="@+id/squareTwo"
    android:layout_alignRight="@+id/squareTwo"
    android:layout_alignTop="@+id/squareFour"
    android:background="@android:color/transparent"
    android:contentDescription="@string/middle"
    android:src="@drawable/blank_button" />

<ImageButton
    android:id="@+id/squareSix"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/squareThree"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@+id/squareFive"
    android:layout_marginBottom="178dp"
    android:background="@android:color/transparent"
    android:contentDescription="@string/middle_right"
    android:src="@drawable/blank_button" />

<ImageButton
    android:id="@+id/squareSeven"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignRight="@+id/squareFour"
    android:layout_below="@+id/squareFive"
    android:layout_marginBottom="70dp"
    android:layout_marginTop="10dp"
    android:background="@android:color/transparent"
    android:contentDescription="@string/bottom_left"
    android:src="@drawable/blank_button" />

<ImageButton
    android:id="@+id/squareEight"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/squareSeven"
    android:layout_alignLeft="@+id/squareFive"
    android:layout_alignRight="@+id/squareFive"
    android:layout_alignTop="@+id/squareSeven"
    android:background="@android:color/transparent"
    android:contentDescription="@string/bottom"
    android:src="@drawable/blank_button" />

<ImageButton
    android:id="@+id/squareNine"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/squareEight"
    android:layout_alignLeft="@+id/squareSix"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@+id/squareEight"
    android:background="@android:color/transparent"
    android:contentDescription="@string/bottom_right"
    android:src="@drawable/blank_button" />

</RelativeLayout>

我使用一个blank_button.png 文件并将它拉伸到gameBoard ImageView 上的每个方格上。我知道我从所有讨厌的人那里知道我应该改用相对布局,但我只是希望这个应用程序可以在特定类型的设备上运行。

【问题讨论】:

  • stackoverflow.com/questions/4755871/…也许这对你有帮助?
  • 我明白你在说什么。我尝试制作两个单独的 xml 文件,一个用于按钮状态,一个用于突出显示的图像。我尝试用android:tileMode="repeat" 平铺小方块。我尝试在布局文件中引用它。但是,这只是在每个较大的展开按钮上放置一个小方块。这仍然不能解决问题。

标签: java android xml android-layout imagebutton


【解决方案1】:

通过xml,您可以通过google搜索。我将向您展示一种解决问题的方法。 A forum which mentioned this problem

当然,如果你不使用xml,你必须把它改成java代码。很简单,但这意味着它不会显示复杂的效果。

补充:重写onStateChange方法,你应该重写你的widget。

【讨论】:

  • 我知道如何通过java解决它。我只想在调用 setContentView 时将其预加载到 onCreate 上。必须通过xml来解决这个问题,我已经google了一段时间,所以我希望有丰富经验的人可以回答这个问题。
猜你喜欢
  • 2013-07-26
  • 2022-11-30
  • 2020-05-21
  • 2013-04-12
  • 2021-05-02
  • 1970-01-01
  • 2021-07-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多