gpt4 book ai didi

Android 布局焦点问题 : layout clickable, 但其元素窃取焦点

转载 作者:行者123 更新时间:2023-11-30 01:54:26 25 4
gpt4 key购买 nike

这是我的 android 问题。

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

<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>

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

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

鉴于布局充满了按钮和 TextView ,全部设置为可点击和可聚焦的错误,有没有办法强制布局成为点击的接收者而不是其元素之一?因为它似乎是一个链接到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 中使用此布局。

关于Android 布局焦点问题 : layout clickable, 但其元素窃取焦点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32498305/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com