【问题标题】:Issue with onclicklistener to LinearLayout线性布局的 onclicklistener 问题
【发布时间】:2013-03-06 20:52:15
【问题描述】:

为了将图标和文本组合在一起,我将它们组合在一个线性布局中,并为线性布局实现了一个监听器。

<LinearLayout
        android:id="@+id/ll0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="fill_horizontal"
        android:orientation="vertical" >
        <ImageButton
            android:id="@+id/imageButton0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@null"
            android:src="@drawable/start" />
        <TextView
            android:id="@+id/textView0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Start"
            android:textAppearance="?android:attr/textAppearanceMedium" />
    </LinearLayout>

我已经实现了监听器,方式如下:-

l0 = (LinearLayout)findViewById(R.id.ll0);
l0.setOnClickListener(new View.OnClickListener() 
    {
        public void onClick(View v) 
        {
            //Some Code
        }
    });

我面临的问题是我点击图标,听者似乎没有响应。当我单击文本视图和图标之间的空间时,侦听器工作。我希望整个部分都是可点击的,而不是在某个特定点。

【问题讨论】:

    标签: android android-linearlayout


    【解决方案1】:

    我认为ImageButton 是一个可点击的视图,并且正在捕获点击,从而阻止了 LinearLayout 接收点击事件。尝试将android:clickable="false" 添加到定义ImageButton 的XML 中。

    但是,更好的答案是使用复合可绘制对象。见How do I use a compound drawable instead of a LinearLayout that contains an ImageView and a TextView。基本上,您可以将android:drawableTop="@drawable/start" 添加到定义TextView 的XML 中,并完全取消LinearLayoutImageButton。然后你只需处理点击TextView

    【讨论】:

    • android:clickable 似乎没有帮助,但复合可绘制效果很好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-31
    相关资源
    最近更新 更多