Android的RelativeLayout的变色的onClick [英] Android RelativeLayout change color onClick

查看:137
本文介绍了Android的RelativeLayout的变色的onClick的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要如何改变一个相对布局我作为点击像普通按钮可点击使用的颜色? 就像我希望有一个视觉反馈的布局是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

希望它可以帮助

这篇关于Android的RelativeLayout的变色的onClick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆