27

我想创建一个类似于 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 的样式中,我为所有状态设置了一个选择器。

现在:

  • 当我单击 ImageView 时(我也使用 ImageButton 进行了尝试)-它的行为正确并且图像根据选择器 xml 进行了更改。
  • 当我单击 LinearLayout - 单击线性布局,但 ImageView 和 TextView 不会改变它的可绘制/外观

所以我想做以下事情。当我单击父 LinearLayout 时,我需要将它的所有子项更改为按下状态。

我尝试将以下代码添加到 LinearLayout onClickListener 以传播点击:

@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();
    }
}

但它仍然是一样的。非常感谢您的帮助。

4

5 回答 5

53

android:duplicateParentState="true"

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

于 2012-04-16T08:59:42.110 回答
7

Not only make for every child:

android:duplicateParentState="true"

But also additionally:

android:clickable="false"  

This will prevent unexpected behaviour (or solution simply not working) if clickable child views are used.

SO Source

于 2017-05-27T18:14:20.743 回答
1

After having the same problem some months later, I found this solution:

private void setOnClickListeners() {
    super.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            onClick(v);
        }
    });
    for (int index = 0; index < super.getChildCount(); index++) {
        View view = super.getChildAt(index);
        view.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                onClick(v);
            }
        });
    }
}

protected void onClick(View v) {
    // something to do here...
}
于 2012-11-26T07:59:12.323 回答
1

In my case, no one of the other solutions works!

I finally had to use OnTouchListener as explained here, capturing the event when the user clicks in the parent view, and removing all childs OnClickListener.

So the idea is, delegate the click behavior to the parent, and notify the child that is really clicked, if you want to propagate the event. ¡¡That's what we are looking for!!

Then, we need to check which child has been clicked. You can find a reference here to know how it´s done. But the idea is basiclly getting the area of the child, and asking for who contains the clicked coordinates, to perform his action (or not).

于 2018-05-17T17:16:49.510 回答
0

At first, my child view failed to get click from parent. After investigating, what I need to do to make it work are:

  1. remove click listener on child view
  2. adding click listener on parent view

So, I don't need to add these on every children.

android:duplicateParentState="true"
android:clickable="false"

I only add duplicateParentState to one of my child view.

My child view is now listening to parent click event.

于 2019-02-21T19:49:53.217 回答