在LinearLayout中选择颜色 [英] Selector color on LinearLayout

查看:100
本文介绍了在LinearLayout中选择颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想assing颜色选择器的扩展类的LinearLayout的,所以,我认为它一样,如果我们谈论的LinearLayout。

I'm trying to assing a color selector to an extended class of LinearLayout, so, i think its like if we speak about linearLayout.

我也跟着在<一的说明href="http://stackoverflow.com/questions/3506319/android-linearlayout-with-color-resource-what-am-i-doing-wrong">this后,得到的答案谈论的形状。

i followed the instructions on this post, the answer talking about shapes.

现在我有3 XML在可绘文件夹:

Now i have 3 xml on drawables folders:

normal.xml文件

normal.xml file

<shape xmlns:android="http://schemas.android.com/apk/res/android"
  android:shape="rectangle" >
  <solid android:color="#ffffffff" />
</shape>

pressed.xml文件

pressed.xml file

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
   <solid android:color="#00000000" />
</shape>

最后,bg.xml文件

and finally, bg.xml file

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/pressed" />
    <item android:state_focused="true" android:drawable="@drawable/pressed" />
    <item android:state_selected="true" android:drawable="@drawable/pressed" />
    <item android:drawable="@drawable/normal" />
</selector>

我访问此以下列方式:

I am accessing this in the following way:

    Drawable d = getResources().getDrawable(context.getResources().getIdentifier("mypackageuri.tProject:drawable/bg", null, null));
    view.setBackgroundDrawable(d);

正常状态的细末,用颜色设置为normal.xml,但是没办法跟其他的人,我preSS我的观点并没有任何反应,它不改变颜色以任何方式..

The "normal" state its fine, with the color set at "normal.xml", but no way with the other ones, I press my view and nothing happens, it's not changing color in any way...

我不明白我在做什么错了......

I can't see what i'm doing wrong...

感谢您

推荐答案

您认为需要可以点击,以获得国家 pressed 当你点击它。 用途:

Your view needs to be clickable in order to get the state pressed when you click on it. Use :

    view.setClickable(true);

或在布局的xml:

or in the layout xml :

    android:clickable="true"

这篇关于在LinearLayout中选择颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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