【问题标题】:Android layout not clickable when include ImageView and TextView包含 ImageView 和 TextView 时,Android 布局不可点击
【发布时间】:2018-06-29 19:23:30
【问题描述】:

我有如下布局。共有五个类似的选项卡。当我在代码中将 onClickListener 设置为 LinearLayout tab_dashboard 时,没有任何触发。我还必须将点击侦听器设置为 ImageButton 和 TextView 以使整个区域可点击。我试图设置所有子项 clickable="false" 和 duplicateParentState="true",没有运气。我还尝试将 ImageButton 更改为 ImageView,同样的问题。有人对此有想法吗?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <LinearLayout
        android:id="@+id/tab_dashboard"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@android:color/white"
        android:gravity="center"
        android:orientation="vertical">

        <ImageButton
            android:id="@+id/dashboard_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:background="@android:color/white"
            android:src="@drawable/ic_dashboard" />

        <TextView
            android:id="@+id/dashboard_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:gravity="center_horizontal"
            android:text="@string/dashboard"
            android:textSize="10sp" />

    </LinearLayout>

    <!-- Four more similar tabs -->

</LinearLayout

【问题讨论】:

    标签: android android-layout clickable android-imagebutton


    【解决方案1】:

    在 TextView 和 ImageView 中添加 android:clickable="false"android:focusable="false"

    在您的活动中添加以下代码。

    ((ImageView) findViewById(R.id.dashboard_button)).setOnClickListener(null);
    ((ImageView) findViewById(R.id.dashboard_button)).setFocusable(false);
    ((TextView) findViewById(R.id.dashboard_text)).setOnClickListener(null);
    ((TextView) findViewById(R.id.dashboard_text)).setFocusable(false);
    

    【讨论】:

      【解决方案2】:

      我通过将 android:layout_width="0dp" 和 android:layout_weight="1" 替换为达到相同效果的其他方式解决了这个问题。我猜 android:layout_width="0dp" 可能是这个问题的原因。稍后会做更多调查。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-01-02
        • 1970-01-01
        • 2014-02-14
        • 1970-01-01
        • 2019-08-03
        • 2014-11-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多