【问题标题】:Fragment doesn't overlay whole layout片段不覆盖整个布局
【发布时间】:2018-07-18 11:54:23
【问题描述】:

我有一个底部导航栏和一个浏览器。 在一个片段中,有一个带有项目的回收站视图。在项目上单击它需要打开该项目的详细信息屏幕。

   addFragment(DetailsFragment(),R.id.container)

这是一个扩展。

fun AppCompatActivity.addFragment(fragment: Fragment, frameId: Int) {
    supportFragmentManager.inTransaction { add(frameId, fragment).addToBackStack(null) }
}

这是我的 activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

        <com.product.NonSwipableViewPager
            android:id="@+id/viewPagerFragments"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toTopOf="@+id/navigation"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <android.support.design.widget.BottomNavigationView
            android:id="@+id/navigation"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="0dp"
            android:layout_marginStart="0dp"
            android:background="?android:attr/windowBackground"
            app:itemIconTint="@drawable/selector_bottom_navigation"
            app:itemTextColor="@drawable/selector_bottom_navigation"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:menu="@menu/navigation" />


</android.support.constraint.ConstraintLayout>

有什么解决办法吗?我尝试在此容器内创建另一个容器,但它不起作用。

一种解决方案是使用另一个活动。我只是想知道是否可以使用 Fragment 或者我做错了什么。

【问题讨论】:

  • 有什么解决办法吗?
  • 试试android.R.id.content
  • 解决方案用另一个片段覆盖整个活动,因此底部导航视图将被隐藏。因为现在它总是显示出来。
  • 阿尔班你的解决方案奏效了:)

标签: android android-fragments kotlin bottomnavigationview


【解决方案1】:

按照步骤进行

第 1 步:从 ConstraintLayout 中剪下这条线

android:id="@+id/container"

第 2 步:添加这些行

<FrameLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</FrameLayout>

您的最终代码将如下所示

<android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

     <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </FrameLayout>

    <com.product.NonSwipableViewPager
        android:id="@+id/viewPagerFragments"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/navigation"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />



    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="0dp"
        android:layout_marginStart="0dp"
        android:background="?android:attr/windowBackground"
        app:itemIconTint="@drawable/selector_bottom_navigation"
        app:itemTextColor="@drawable/selector_bottom_navigation"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/navigation" />

【讨论】:

  • 试过了,结果一样。
【解决方案2】:

对我来说使用R.id.content 有效

【讨论】:

    猜你喜欢
    • 2020-02-07
    • 1970-01-01
    • 2021-07-07
    • 1970-01-01
    • 1970-01-01
    • 2013-07-01
    • 2016-03-10
    • 1970-01-01
    • 2016-11-06
    相关资源
    最近更新 更多