【问题标题】:Clicking on Fragment goes through in Activity在 Activity 中点击 Fragment
【发布时间】:2019-02-12 21:10:34
【问题描述】:

我的MainActivity 中有一个按钮,可以打开FragmentAFragmentA覆盖了整个屏幕,但我仍然看到来自MainActivity的按钮,我仍然可以点击它。

我尝试在我的片段布局中使用clickable,但它不起作用

MainActivity

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()
        }
    }

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

fragment_a.xml

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

【问题讨论】:

    标签: java android android-fragments kotlin transparent


    【解决方案1】:

    发生这种情况是因为您将 Button 放置在您用作 Fragment 容器的 ConstraintLayout 内。

    当您将add 片段添加到容器中时,就像您正在做的那样,它只是以与View 相同的方式添加它。

    因此,如果您将 Fragment 添加到已拥有 ButtonConstraintLayout 中,则由于 ConstraintLayout 允许重叠视图,因此该 Fragment 将显示在 Button 旁边。

    这也是为什么,如果您的容器是 LinearLayout,那么添加片段会将片段放置在您的 Button 下方。

    因此,考虑到这一点,解决方案是将其视为视图来处理。

    如果您在布局中添加了一个视图,并且您有另一个视图重叠,您将如何摆脱它?

    最常见的解决方案是在添加 Fragment 时将 Button 的可见性设置为 INVISIBLE 或 GONE。

    另一种解决方案可能是提高 Fragment 的高度,因此它现在高于您的 Button

    当然,您也可以从 Container 中移除按钮并将其放入 Fragment 中。

    这样,您可以使用FragmentManager 中的replace() 方法将包含您的Button 的片段替换为您要显示的Fragment

    【讨论】:

    • 谢谢,每当backstackEntryCount != 0@ 时,我都会隐藏按钮
    • 对我来说,更改布局或添加高度不起作用,但检查 backstack 计数以防止点击就可以了。谢谢@Barcode
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-14
    • 2014-12-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多