如何防止触摸影响其父级的片段 activity

How to prevent touch from a fragment affecting its parent activity

我有一个从 activity 启动的片段。现在我注意到当一个空白区域被触摸时,父 activity 被触摸。只有按钮不受影响的区域。它的行为就好像它是透明的。

有什么办法可以避免吗?

父级 activity XML:

<?xml version="1.0" encoding="utf-8" ?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@id/layout_dashboard_parent"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/cardview_light_background">

    <ScrollView
        android:id="@id/main_scroll_view"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:fillViewport="true">

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

            <FrameLayout
                android:id="@id/dashboard_alert_section"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:visibility="gone">

                <include layout="@layout/dashboard_feature_tip_item" />
            </FrameLayout>

            <LinearLayout
                android:id="@id/main_control_layout"
                android:layout_width="fill_parent"
                android:layout_height="611dp">

                <LinearLayout
                    android:id="@id/layout_main"
                    style="@style/CardView.Dark"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:background="@color/c_main_back_ground_color"
                    android:gravity="bottom"
                    android:orientation="vertical">

                    <RelativeLayout
                        android:id="@+id/layout_balance_check"
                        android:layout_width="fill_parent"
                        android:layout_height="120.0dip"
                        android:layout_marginBottom="6.0dip"
                        android:layout_marginEnd="@dimen/dashboard_feature_end_margin_usa"
                        android:layout_marginLeft="@dimen/dashboard_feature_start_margin_usa"
                        android:layout_marginRight="@dimen/dashboard_feature_end_margin_usa"
                        android:layout_marginStart="@dimen/dashboard_feature_start_margin_usa"
                        android:layout_marginTop="@dimen/dashboard_feature_top_margin_usa"
                        android:layout_weight="1.0"
                        android:background="@color/battery_charging_icon_color"
                        android:clickable="true"
                        android:elevation="@dimen/dashboard_feature_bg_elevation"
                        android:focusable="true"
                        android:focusableInTouchMode="false"
                        android:theme="@style/Base.Theme.AppCompat"
                        tools:targetApi="lollipop">

                        <include
                            layout="@layout/sub_layout_account_cb"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content" />

                        <View
                            android:id="@+id/view"
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent"
                            android:background="?android:selectableItemBackground"
                            android:duplicateParentState="true" />

                    </RelativeLayout>

                    <RelativeLayout
                        android:id="@+id/layout_balance_load"
                        android:layout_width="fill_parent"
                        android:layout_height="120.0dip"
                        android:layout_marginBottom="6.0dip"
                        android:layout_marginEnd="@dimen/dashboard_feature_end_margin_usa"
                        android:layout_marginLeft="@dimen/dashboard_feature_start_margin_usa"
                        android:layout_marginRight="@dimen/dashboard_feature_end_margin_usa"
                        android:layout_marginStart="@dimen/dashboard_feature_start_margin_usa"
                        android:layout_weight="1.0"
                        android:background="@color/white"
                        android:clickable="true"
                        android:elevation="@dimen/dashboard_feature_bg_elevation"
                        android:focusable="true"
                        tools:targetApi="lollipop">

                        <include
                            layout="@layout/sub_layout_account_lb"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content" />

                        <View
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent"
                            android:background="?android:selectableItemBackground"
                            android:duplicateParentState="true" />
                    </RelativeLayout>

                    <RelativeLayout
                        android:id="@+id/layout_credit_share"
                        android:layout_width="fill_parent"
                        android:layout_height="120.0dip"
                        android:layout_marginBottom="6.0dip"
                        android:layout_marginEnd="@dimen/dashboard_feature_end_margin_usa"
                        android:layout_marginLeft="@dimen/dashboard_feature_start_margin_usa"
                        android:layout_marginRight="@dimen/dashboard_feature_end_margin_usa"
                        android:layout_marginStart="@dimen/dashboard_feature_start_margin_usa"
                        android:layout_weight="1.0"
                        android:background="@color/white"
                        android:clickable="true"
                        android:elevation="@dimen/dashboard_feature_bg_elevation"
                        android:focusable="true"
                        tools:targetApi="lollipop">

                        <include
                            layout="@layout/sub_layout_account_sc"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content" />

                        <View
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent"
                            android:background="?android:selectableItemBackground"
                            android:duplicateParentState="true" />
                    </RelativeLayout>

                    <RelativeLayout
                        android:id="@+id/layout_credit_barrow"
                        android:layout_width="fill_parent"
                        android:layout_height="120.0dip"
                        android:layout_marginBottom="6.0dip"
                        android:layout_marginEnd="@dimen/dashboard_feature_end_margin_usa"
                        android:layout_marginLeft="@dimen/dashboard_feature_start_margin_usa"
                        android:layout_marginRight="@dimen/dashboard_feature_end_margin_usa"
                        android:layout_marginStart="@dimen/dashboard_feature_start_margin_usa"
                        android:background="@color/white"
                        android:clickable="true"
                        android:elevation="@dimen/dashboard_feature_bg_elevation"
                        android:focusable="true"
                        android:visibility="visible"
                        tools:targetApi="lollipop">

                        <TextView
                            android:id="@+id/tv_borrow_credit"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignLeft="@+id/tv_borrow_credit_warning"
                            android:layout_alignStart="@+id/tv_borrow_credit_warning"
                            android:layout_alignTop="@+id/imageView3"
                            android:text="@string/borrow_credit"
                            android:textColor="@color/c_main_back_ground_color"
                            android:textSize="18sp"
                            android:textStyle="bold" />

                        <TextView
                            android:id="@+id/tv_borrow_credit_warning"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignBottom="@+id/imageView3"
                            android:layout_marginLeft="21dp"
                            android:layout_marginStart="21dp"
                            android:layout_toEndOf="@+id/imageView3"
                            android:layout_toRightOf="@+id/imageView3"
                            android:background="@color/c_main_back_ground_color"
                            android:paddingEnd="5dip"
                            android:paddingLeft="5dip"
                            android:paddingRight="5dip"
                            android:paddingStart="5dip"
                            android:text="@string/borrow_credit_warning"
                            android:textAppearance="@style/TextAppearance.AppCompat"
                            android:textColor="?android:attr/editTextColor"
                            android:textSize="12sp" />

                        <ImageView
                            android:id="@+id/imageView3"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentLeft="true"
                            android:layout_alignParentStart="true"
                            android:layout_centerVertical="true"
                            android:layout_marginLeft="21dp"
                            android:layout_marginStart="21dp"
                            android:contentDescription=""
                            app:srcCompat="@drawable/engaging" />

                        <View
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent"
                            android:layout_alignParentLeft="true"
                            android:layout_alignParentStart="true"
                            android:layout_alignParentTop="true"
                            android:background="?android:selectableItemBackground"
                            android:duplicateParentState="true" />
                    </RelativeLayout>
                </LinearLayout>
            </LinearLayout>
        </LinearLayout>
    </ScrollView>

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="10dip"
        android:layout_gravity="bottom"
        android:elevation="5dp"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        tools:targetApi="lollipop" />

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:animationCache="true">

    </FrameLayout>

    <FrameLayout
        android:id="@+id/fragment_container_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="false"
        android:layout_centerInParent="true"
        android:animateLayoutChanges="true"
        android:animationCache="true">

    </FrameLayout>

</RelativeLayout>

片段XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/africell_cb_int"
    android:layout_width="410dp"
    android:layout_height="wrap_content"
    android:background="?android:attr/textColorLinkInverse"
    android:baselineAligned="false"
    android:orientation="vertical"
    android:padding="10.0dip"
    android:weightSum="1.0">


    <TextView
        android:id="@+id/textView8"
        android:layout_width="332dp"
        android:layout_height="97dp"
        android:layout_marginBottom="30dip"
        tools:ignore="HardcodedText" />

    <TextView
        android:id="@+id/textView6"
        android:layout_width="330dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="30dip"
        android:text="@string/load_balance_dialog"
        android:textAlignment="center"
        android:textColor="@color/black"
        android:textSize="18sp" />

    <android.support.design.widget.TextInputLayout
        android:layout_width="330dp"
        android:layout_height="match_parent"
        android:background="@color/action_button_color"
        android:clickable="false">

        <EditText
            android:id="@+id/inputField"
            style="@style/Widget.AppCompat.EditText"
            android:layout_width="322dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:gravity="center_horizontal"
            android:hint="@string/edit_text_hint"
            android:inputType="number"
            android:textAlignment="textEnd"
            android:textColor="@color/primary_text_color"
            android:textStyle="bold"
            android:maxLines="1" />

    </android.support.design.widget.TextInputLayout>

    <LinearLayout
        android:layout_width="330dp"
        android:layout_height="match_parent"
        android:layout_marginTop="15dp"
        android:orientation="horizontal">

        <Button
            android:id="@+id/cancel"
            style="@style/Widget.AppCompat.Button.ButtonBar.AlertDialog"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="10dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="10dp"
            android:layout_marginStart="5dp"
            android:layout_weight="0.39"
            android:text="@string/cancel"
            android:textColor="@android:color/black" />

        <Button
            android:id="@+id/confirm"
            style="@style/Widget.AppCompat.Button.ButtonBar.AlertDialog"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="5dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="10dp"
            android:layout_marginStart="10dp"
            android:layout_weight="0.36"
            android:text="@string/confirm"
            android:textColor="@android:color/black" />

    </LinearLayout>


</LinearLayout>

是的,您必须让您的根视图可点击。只需设置 属性 clickable true 甚至不需要为此写空 OnClickListener

android:clickable="true"

您的片段 xml 具有 ID 为 africell_cb_int 的根视图。使其可点击。

在 Fragment Xml 布局中为根标签添加 android:clickable="true"

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true" />