首页 > 解决方案 > 切换到另一个片段时如何删除某些布局获取的布局空间?

问题描述

我在图像点击时从 A 片段转到 B 片段。在我的 A 片段中,我有底部导航视图。当我到达片段 B 时,我想删除它的视图和空间获取。为了实现这一点,我在我的活动中使用了这个代码。

public void onBackStackChanged() {
        Fragment f = getSupportFragmentManager().findFragmentById(R.id.fragment_container_dashboard);

        if (f instanceof ExcitingOfferFragment) {
            bottomNavigationView.setVisibility(View.GONE);
        }

         else {
            bottomNavigationView.setVisibility(View.VISIBLE);
        }
}

但问题是通过这段代码,我的底部导航视图只会变得不可见并且它保持它的空间。

这是这是片段 B 的屏幕截图。您可以清楚地看到我的底部导航视图是不可见的,但它的空间已获得。 我也用过 GONE。 当底部导航视图消失时,我想要在 Fragment B 中Fragment B的 XML 布局代码

<?xml version="1.0" encoding="utf-8"?>

<androidx.coordinatorlayout.widget.CoordinatorLayout 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:background="#fafafa"
    android:overScrollMode="never">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <View
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:background="#ff9800" />

        <androidx.core.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:overScrollMode="never"
            android:scrollbars="none"
            android:scrollingCache="true">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">


                <androidx.cardview.widget.CardView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:cardCornerRadius="2dp"
                    app:cardElevation="2dp">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical">

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_margin="@dimen/spacing_large"
                            android:clickable="true"
                            android:orientation="horizontal">

                            <RelativeLayout
                                android:layout_width="30dp"
                                android:layout_height="30dp">

                                <ImageView
                                    android:id="@+id/m_eo_details_img"
                                    android:layout_width="30dp"
                                    android:layout_height="30dp"
                                    android:scaleType="fitXY"
                                    app:srcCompat="@drawable/app_logo" />

                                <ProgressBar
                                    android:id="@+id/eo_progress"
                                    android:layout_width="30dp"
                                    android:layout_height="30dp"
                                    android:visibility="visible" />

                                <ImageView
                                    android:id="@+id/eo_failed_img"
                                    android:layout_width="30dp"
                                    android:layout_height="30dp"
                                    android:src="@drawable/app_logo"
                                    android:visibility="gone" />

                            </RelativeLayout>

                            <View
                                android:layout_width="10dp"
                                android:layout_height="0dp" />

                            <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:orientation="vertical">

                                <LinearLayout
                                    android:layout_width="match_parent"
                                    android:layout_height="match_parent"
                                    android:orientation="horizontal">

                                    <LinearLayout
                                        android:layout_width="0dp"
                                        android:layout_height="match_parent"
                                        android:layout_weight="1"
                                        android:orientation="vertical">

                                        <TextView
                                            android:id="@+id/m_eo_title"
                                            android:layout_width="match_parent"
                                            android:layout_height="match_parent"
                                            android:fontFamily="@font/calibri"
                                            android:gravity="center_vertical|left"
                                            android:text="Loading"
                                            android:marqueeRepeatLimit="marquee_forever"
                                            android:scrollHorizontally="true"
                                            android:ellipsize="marquee"
                                            android:focusable="true"
                                            android:focusableInTouchMode="true"
                                            android:textColor="#000000"
                                            android:textSize="20sp"
                                            android:textStyle="bold" />

                                    </LinearLayout>

                                </LinearLayout>
                            </LinearLayout>

                        </LinearLayout>

                    </LinearLayout>

                </androidx.cardview.widget.CardView>


                <androidx.cardview.widget.CardView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="@dimen/spacing_middle"
                    android:layout_marginTop="15dp"
                    android:layout_marginRight="@dimen/spacing_middle"
                    app:cardBackgroundColor="@android:color/white"
                    app:cardCornerRadius="2dp"
                    app:cardElevation="2dp">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical">

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_margin="@dimen/spacing_large"
                            android:orientation="vertical">

                            <TextView
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:fontFamily="@font/calibri"
                                android:text="Deal Description:-"
                                android:textColor="#000000"
                                android:textSize="16sp"
                                android:textStyle="bold" />

                            <TextView
                                android:id="@+id/m_eo_description"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_marginTop="@dimen/spacing_middle"
                                android:fontFamily="@font/helvetica"
                                android:text="Loading"
                                android:textColor="#000000"
                                android:textSize="20sp" />

                        </LinearLayout>

                    </LinearLayout>

                </androidx.cardview.widget.CardView>

                <androidx.cardview.widget.CardView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="@dimen/spacing_middle"
                    android:layout_marginTop="15dp"
                    android:layout_marginRight="@dimen/spacing_middle"
                    app:cardBackgroundColor="@android:color/white"
                    app:cardCornerRadius="2dp"
                    app:cardElevation="2dp">

                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">

                        <TextView
                            android:id="@+id/text"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="6dp"
                            android:layout_marginBottom="6dp"
                            android:fontFamily="@font/calibri"
                            android:gravity="center"
                            android:text="Coupon Code"
                            android:textAlignment="center"
                            android:textColor="#000000"
                            android:textSize="16sp"
                            android:textStyle="bold" />

                        <TextView
                            android:id="@+id/m_eo_coupon_code"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_below="@+id/text"
                            android:layout_marginBottom="4dp"
                            android:fontFamily="@font/calibri"
                            android:gravity="center"
                            android:padding="4dp"
                            android:text="NOT REQUIRED"
                            android:textAlignment="center"
                            android:textAllCaps="true"
                            android:textColor="#308fe9"
                            android:textSize="25sp" />

                        <View
                            android:id="@+id/viewgery"
                            android:layout_width="match_parent"
                            android:layout_height="2dp"
                            android:layout_below="@+id/m_eo_coupon_code"
                            android:background="#A9A9A9" />

                        <TextView
                            android:id="@+id/m_eo_copyCode"
                            android:layout_width="wrap_content"
                            android:layout_height="30dp"
                            android:layout_below="@+id/viewgery"
                            android:layout_centerHorizontal="true"
                            android:layout_gravity="center"
                            android:gravity="center"
                            android:text="COPY CODE"
                            android:textAlignment="center"
                            android:textAllCaps="true"
                            android:textSize="18sp" />


                    </RelativeLayout>

                </androidx.cardview.widget.CardView>

                <View
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/spacing_mlarge" />

            </LinearLayout>

        </androidx.core.widget.NestedScrollView>

        <com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto"
            android:id="@+id/adView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="3dp"
            ads:adSize="SMART_BANNER"
            ads:adUnitId="ca-app-pub-XXXXXX"></com.google.android.gms.ads.AdView>

        <View
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:background="#ff9800" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="@dimen/spacing_xxlarge"
            android:background="@color/colorPrimary"
            android:gravity="center_vertical"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/m_eo_target"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/colorPrimary"
                android:fontFamily="@font/calibri"
                android:gravity="center"
                android:text="GO TO STORE"
                android:textAlignment="center"
                android:textAllCaps="true"
                android:textAppearance="@style/TextAppearance.AppCompat.Subhead"
                android:textColor="#ffffff"
                android:textSize="22sp" />


        </LinearLayout>

    </LinearLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

底部导航视图 XML..

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:layout_above="@+id/bottomNavi"
        android:background="#ff9800"></View>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottomNavi"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:layout_alignParentBottom="true"
        android:elevation="2dp"
        android:visibility="visible"
        android:background="@color/colorPrimary"
        app:itemIconTint="@color/botto_nav_color"
        app:itemTextColor="@color/botto_nav_color"
        app:menu="@menu/bottom_nav_menu"/>

</RelativeLayout>

标签: javaandroid

解决方案


你可以试试

bottomNavigationView.RequestLayout()

或者更可能获取bottomNavigationView 的父视图并调用其RequestLayout 方法。


推荐阅读