Android的RelativeLayout的变色的onClickAndroid、RelativeLayout、onClick

2023-09-05 10:04:21 作者:自由如风

我要如何改变一个相对布局我作为点击像普通按钮可点击使用的颜色? 就像我希望有一个视觉反馈的布局是pressed。

How do i change the color of a Relative Layout i use as a clickable on Click like the normal Button? Like i want a visual feedback the layout was pressed.

我试了一下,这必然让背景属性这样一个选择:

I tried it with a selector bound to the background property like this:

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

,并用它在布局底色......

and used it in the Layouts backround...

安卓背景=@色/ layout_selector

但是这给了我一个充气异常...

but this gives me an Inflate Exception...

任何想法?

推荐答案

请尝试以下步骤:

在水库 - >值文件夹中创建的 color.xml 与内容:

In res --> values folder create color.xml with the content:

<?xml version="1.0" encoding="utf-8"?>
<resources>     
    <color name="black">#000000</color> 
    <color name="white">#ffffff</color>
</resources>

由于&LT;项目&GT; 在选择标签需要绘制属性或子标签定义绘制,您的 layout_selector.xml 文件(这是保存在水库 - >绘制)应该是这样的:

As <item> tag in selector requires a drawable attribute or child tag defining a drawable, your layout_selector.xml file (which is saved in res --> drawable) should look like this:

<?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/black"/> 
    <item android:state_pressed="true" android:state_enabled="false" android:drawable="@color/black" />
    <item android:drawable="@color/white"/> 
 </selector>

另外,正如前面所说的,相对布局应可点击(机器人:可点击=真正的

和它的背景设置为安卓背景=@可绘制/ layout_selector

希望它可以帮助