关闭 DialogFragment 时布局拉伸

Layout stretching when dismissing DialogFragment

我有一个 Activity 具有以下布局:

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:id="@+id/top_menu_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="16dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">

        // ICONS

    </RelativeLayout>

    <FrameLayout
        android:id="@+id/temperatura_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="184dp"
        android:layout_centerHorizontal="true">

        <com.github.pavlospt.CircleView xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/reading_value"
            android:layout_width="192dp"
            android:layout_height="192dp"
            android:layout_gravity="center"
            />
    </FrameLayout>

    <ImageView
        android:id="@+id/add_item_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/bottomBar"
        android:layout_centerHorizontal="true"
        android:padding="12dp"
        android:layout_marginTop="36dp"
        android:background="?attr/selectableItemBackgroundBorderless"
        android:clickable="true"
        android:focusable="true"
        android:scaleType="center"
        app:srcCompat="@drawable/ic_add_item_black_24px"
        android:contentDescription="@string/add_item_btn_description" />

    <com.roughike.bottombar.BottomBar
        android:id="@+id/bottomBar"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_alignParentBottom="true"
        app:bb_behavior="iconsOnly"
        app:bb_tabXmlResource="@xml/bottom_navbar" />

</RelativeLayout>

add_item_btn 有一个点击事件,创建 全屏 (编辑以添加此信息) DialogFragment插入商品信息,添加到本地数据库。以下代码显示了我如何创建 DialogFragment:

FragmentManager fragmentManager = getSupportFragmentManager();
InsertItemDialogFragment fragment = new InsertItemDialogFragment();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
transaction.add(android.R.id.content, fragment).addToBackStack(null).commit();

问题是,当我 dismiss() 创建的片段时,activity 布局会拉伸,BottomBar 会在屏幕按钮栏后面一半(请参阅图片)。

我尝试更改 FragmentTransaction(从 android.R.id.contentR.id.main_content,而 main_content 是根 RelativeLayout 的 ID)但没有成功。我还尝试使用 Material Design Bottom Navigation Bar 的 2 种不同实现,但两者具有相同的行为。

找到答案了!我实际上不知道为什么,因为我几乎没有使用 CoordinatorLayout 的经验,但是将 FragmentDialog 的根布局切换为 LinearLayout 解决了问题。

首先:您正在将 Activity 更改为全屏并调用以添加片段。关闭对话框后,您必须使 activity 恢复其布局,就像将其设置为全屏一样,但这次是普通屏幕。您看到的 space 是 android 的系统使用的 space 栏。

第二:不要使用相对布局。使用线性布局来布置您的 activity 学习使用权重并在需要时使用。

第三:为什么要在对话框片段中使用协调器布局。你不需要。

第四:Android有自己的底栏https://developer.android.com/reference/android/support/design/widget/BottomNavigationView.html用吧。他们甚至有一个工作示例和模板,您可以从 Android Studio 中看到。

根据您提供的信息很难知道您的应用有什么问题