Java 单击片段将在活动中完成
我的Java 单击片段将在活动中完成,java,android,android-fragments,kotlin,transparent,Java,Android,Android Fragments,Kotlin,Transparent,我的main活动中有一个按钮,打开FragmentAFragmentA覆盖了整个屏幕,但我仍然可以看到MainActivity中的按钮,我仍然可以单击它 我试着在片段布局中使用可点击的,但不起作用 main活动 override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main)
main活动中有一个按钮
,打开FragmentA
FragmentA
覆盖了整个屏幕,但我仍然可以看到MainActivity
中的按钮,我仍然可以单击它
我试着在片段布局中使用可点击的,但不起作用
main活动
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
button.setOnClickListener {
val fragmentManager = this@MainActivity.supportFragmentManager
fragmentManager.beginTransaction()
.add(R.id.fragment_container, AFragment())
.addToBackStack(null)
.commit()
}
}
活动\u main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="@+id/fragment_container">
<Button
android:text="Button Main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button" app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
</android.support.constraint.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:focusable="true">
android:background="@color/colorPrimary"
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="A"/>
</LinearLayout>
fragment_a.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="@+id/fragment_container">
<Button
android:text="Button Main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button" app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
</android.support.constraint.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:focusable="true">
android:background="@color/colorPrimary"
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="A"/>
</LinearLayout>
android:background=“@color/colorPrimary”
之所以会发生这种情况,是因为您将按钮
放在约束窗口
中,该窗口将用作片段的容器
当你像你正在做的那样将一个片段添加到一个容器中时,它只是以同样的方式添加它,就像它是一个视图一样
因此,如果您将片段添加到已作为子级拥有按钮的ConstraintLayout
中,由于ConstraintLayout
允许重叠视图,片段将显示在按钮旁边
这也是为什么,如果您的容器是线性布局
,那么添加片段会将片段放在按钮
下面
因此,考虑到这一点,解决办法是将其当作视图来处理
如果将一个视图添加到布局中,而另一个视图重叠,如何消除它
最常见的解决方案是在添加片段时将按钮的可见性设置为不可见或消失
另一个解决方案可能是提高片段的高度,因此它现在高于按钮
当然,您也可以从容器中移除按钮,并将其放置在片段中
这样,您可以使用FragmentManager
中的replace()
方法,将包含按钮的片段替换为要显示的片段 之所以会发生这种情况,是因为您将按钮
放在ConstraintLayout
中,该按钮将用作片段的容器
当你像你正在做的那样将一个片段添加到一个容器中时,它只是以同样的方式添加它,就像它是一个视图一样
因此,如果您将片段添加到已作为子级拥有按钮的ConstraintLayout
中,由于ConstraintLayout
允许重叠视图,片段将显示在按钮旁边
这也是为什么,如果您的容器是线性布局
,那么添加片段会将片段放在按钮
下面
因此,考虑到这一点,解决办法是将其当作视图来处理
如果将一个视图添加到布局中,而另一个视图重叠,如何消除它
最常见的解决方案是在添加片段时将按钮的可见性设置为不可见或消失
另一个解决方案可能是提高片段的高度,因此它现在高于按钮
当然,您也可以从容器中移除按钮,并将其放置在片段中
这样,您可以使用FragmentManager
中的replace()
方法,将包含按钮的片段替换为要显示的片段 谢谢,只要backbackbackentrycount!=0
谢谢,只要BackBackbackEntryCount!=0