java - 切换到另一个片段时如何删除某些布局获取的布局空间?
问题描述
我在图像点击时从 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);
}
}
但问题是通过这段代码,我的底部导航视图只会变得不可见并且它保持它的空间。
<?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>
解决方案
你可以试试
bottomNavigationView.RequestLayout()
或者更可能获取bottomNavigationView 的父视图并调用其RequestLayout 方法。
推荐阅读
- c++ - 如何在合并排序 [C++] 中计算比较次数和移动(交换)次数
- javascript - 为什么我不能在循环中添加 onclick 函数
- node.js - 类型 'false' 不可分配给类型 '((status: number) => boolean) | 未定义的打字稿
- asp.net - 任何减小 DocuSign 复选框和单选按钮大小的解决方法?
- oauth-2.0 - WSO2:是否可以自定义错误消息?{"error_description":"客户端身份验证失败。","error":"invalid_client"}
- shell - 运行 `if 时如何获取退出代码!命令; 那么`?
- javascript - 使用 Lambda 在 DynamoDB 中存储 JSON 请求
- reactjs - 避免使用 ReactJs + Redux + thunk 显示来自另一个用户的数据(旧数据)
- date - 谷歌表格查询两个日期之间的总和
- google-bigquery - 用于查询结果的 BigQuery API 问题