【发布时间】: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 上的每个方格上。我知道我从所有讨厌的人那里知道我应该改用相对布局,但我只是希望这个应用程序可以在特定类型的设备上运行。
【问题讨论】:
-
我明白你在说什么。我尝试制作两个单独的 xml 文件,一个用于按钮状态,一个用于突出显示的图像。我尝试用
android:tileMode="repeat"平铺小方块。我尝试在布局文件中引用它。但是,这只是在每个较大的展开按钮上放置一个小方块。这仍然不能解决问题。
标签: java android xml android-layout imagebutton