繁体   English   中英

仅通过XML(没有其他可绘制对象)单击时如何更改按钮的颜色?

[英]How to change color of button when clicked through XML only (without another drawable)?

我想在按下或聚焦按钮或实际上处于任何状态时更改按钮的颜色。 但是,我不仅要通过XML来实现,而且还希望不使用另一个drawable。 解决此问题的先前问题,例如在不使用两个drawable的情况下按下按钮时将其高亮显示? 以及如何在单击时更改Android中按钮的颜色? 可以通过编程方式在没有可绘制对象的情况下完成,也可以通过XML进行可绘制对象。 是否可以在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的每个正方形上。 我知道所有讨厌的人都知道我应该改用相对布局,但我只想让此应用程序在特定类型的设备上运行。

通过xml,您可以通过google搜索。我将向您展示一种解决问题的方法。 一个提到这个问题的论坛

当然,如果您不使用xml,则必须将其更改为java代码。这很简单,但这意味着它不会显示复杂的效果。

补充:覆盖onStateChange方法,您应该覆盖小部件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM