我有一个从活动中启动的片段。现在我注意到,当触摸空白区域时,会触摸父活动。只有有按钮不受影响的区域。它就像是透明的一样。
有没有办法防止这种情况?
父活动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>
答案 0 :(得分:4)
是的,您必须使您的根视图可以点击。只需设置属性clickable true即使不需要为此写空OnClickListener
。
android:clickable="true"
您的Fragment xml具有标识为africell_cb_int
的根视图。点击它。
答案 1 :(得分:1)
在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" />