Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 单击片段将在活动中完成_Java_Android_Android Fragments_Kotlin_Transparent - Fatal编程技术网

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