Android布局焦点问题:布局可点击,但其元素窃取焦点

Android layout focus issue: layout clickable, but its element steal focus

这是我关于 android 的问题。

我正在使用 xml 文件作为布局模板,我膨胀了几次以获得可显示和可点击布局的列表。此模板布局充满了仅可视的文本视图和按钮(根本没有计划与它们进行交互)。

<ScrollView
        android:id="@+id/scroller"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:fillViewport="true"
        android:layout_above="@+id/bottomcontent">

        <LinearLayout
            android:id="@+id/scrollcontentcontainer"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            //Dynamically added layouts go there

        </LinearLayout>
    </ScrollView>

视觉上没有问题。但我想能够触摸所有这些布局,以便显示包含更多信息的其他片段。

问题是我必须多次触摸屏幕,或者布局中没有元素的特定部分才能使 onclick 方法起作用。

鉴于布局充满了按钮和文本视图,所有设置为 clickable 和 focusable false,有没有办法强制布局成为点击的接收者而不是其元素之一?因为它似乎是链接到 xml 文件的焦点问题...

这里是曾经多次膨胀的布局模板:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@drawable/cornerbackground"
android:clickable="true"
android:focusable="true"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp">

   //buttons views textviews and any random stuff go there

</RelativeLayout>

谢谢!

我遇到过同样的问题。我所做的是,我创建了一个自定义布局,它将接受所有触摸事件,并添加到布局中。

public class CustomRelativeLayout extends RelativeLayout {
    public CustomRelativeLayout(Context context) {
        super(context);
    }

    public CustomRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return true;
    }
}

您需要在 xml 中使用此布局。