Android的:如何传播点击事件的LinearLayout童车,改变他们绘制 [英] Android: How to propagate click event to LinearLayout childs and change their drawable

查看:159
本文介绍了Android的:如何传播点击事件的LinearLayout童车,改变他们绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个将行为类似于ImageButton的线性布局。

I would like to create a linear layout which would behave similarly to ImageButton.

<LinearLayout
    android:id="@+id/container"
    style="?WidgetHomeIconContainer">            

    <ImageView
        android:id="@+id/icon"
        style="?WidgetHomeIcon" />

    <TextView
        android:id="@+id/title"
        style="?WidgetHomeLabel"             
        android:text="@string/title"
        android:textAppearance="?attr/TextHomeLabel" />
</LinearLayout>

在款式的ImageView,TextView的和的LinearLayout,我设置一个选择的所有状态。

In styles of ImageView, TextView and LinearLayout, I set a selectors for all states.

现在:

  • 当我点击ImageView的(我想这也与ImageButton的) - 它行为正确的图像会根据选择的XML改变
  • 当我点击的LinearLayout - 线性布局被点击,但ImageView的和TextView中不会改变它的绘制/外观

所以我想做到以下几点。当我点击父的LinearLayout,我需要改变一切的孩子的,以pressed状态。

So I would like to do the following. When I click on parent LinearLayout, I need to change all it's childs to pressed state.

我尝试添加以下code到的LinearLayout onClickListener传播点击:

I tried to add following code to LinearLayout onClickListener to propagate the click:

@Override
public void onClick(View v)
{
    LinearLayout l = (LinearLayout) v;
    for(int i = 0; i < l.getChildCount(); i++)
    {
        l.getChildAt(i).setClickable(true);
        l.getChildAt(i).performClick();
    }
}

但它仍然reamins相同。非常感谢您的任何帮助。

But it still reamins the same. Thank you very much for any help.

推荐答案

android:duplicateParentState="true"

在您的ImageView和TextView的.. <一href="http://developer.android.com/reference/android/view/View.html#attr_android%3aduplicateParentState">then意见从它的直接父,而不是从自身获得的绘制状态(pssed重点,$ P $等)。

in your ImageView and TextView..then the views get its drawable state (focused, pressed, etc.) from its direct parent rather than from itself.

这篇关于Android的:如何传播点击事件的LinearLayout童车,改变他们绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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