膨胀BottomSheetDialog时替换片段
Posted
技术标签:
【中文标题】膨胀BottomSheetDialog时替换片段【英文标题】:Fragment replaced when inflate BottomSheetDialog 【发布时间】:2021-10-21 13:00:00 【问题描述】:我的活动有一个问题:它有 FragmentContainerView,最初显示主片段,当活动的方法 onCreate 启动时,它显示一个 BottomSheetDialog。当我显示 BottomSheetDialog 时,主页片段消失了。 据我所知问题是替换方法,所以我认为我应该在子片段中插入对话框,但我不知道如何。
这是我的项目:
MainActivity onCreate
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setSubtitle("Workout");
toolbar.setNavigationOnClickListener(v ->
getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainerView, new HomeFragment()).commit();
toolbar.setSubtitle("Workout");
);
bottomSheetDialog = new BottomSheetDialog(this, R.style.BottomSheetTheme);
bottomSheetDialog.setContentView(LayoutInflater.from(getApplicationContext()).
inflate(R.layout.bottom_sheet_layout, findViewById(R.id.bottom_sheet)));
bottomSheetDialog.show();
activitymain.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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_
android:layout_
tools:context=".MainActivity">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragmentContainerView"
android:name="com.example.myproject.Fragments.HomeFragment"
android:layout_
android:layout_
android:layout_marginTop="?actionBarSize"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:layout="@layout/fragment_home" />
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_
android:layout_
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:menu="@menu/items"
app:navigationIcon="@drawable/app_logo"
app:titleTextColor="@color/white"
app:subtitleTextColor="@color/white" />
</androidx.constraintlayout.widget.ConstraintLayout>
bottom_sheet_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/bottom_sheet"
android:layout_
android:layout_
android:background="@drawable/bottom_sheet_background">
<TextView
android:id="@+id/titleView"
android:layout_
android:layout_
android:layout_marginTop="8dp"
android:gravity="center"
android:text="title"
android:textColor="@color/red_700"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/TextView"
android:layout_
android:layout_
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="text"
android:textColor="#E53935"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/titleView" />
</androidx.constraintlayout.widget.ConstraintLayout>
bottom_sheet_backgound.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/white"/>
<corners android:topLeftRadius="20dp" android:topRightRadius="20dp"/>
</shape>
主题.xml
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.MyApp" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/red_700</item>
<item name="colorPrimaryVariant">@color/red_900</item>
<item name="android:background">@color/black</item>
<item name="android:textColor">@color/white</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
<item name="android:listDivider">@drawable/divider</item>
</style>
<style name="BottomSheetTheme" parent="Theme.Design.BottomSheetDialog">
<item name="bottomSheetStyle">@style/BottomSheetStyle</item>
</style>
<style name="BottomSheetStyle" parent="Widget.Design.BottomSheet.Modal">
<item name="background">@android:color/transparent</item>
</style>
</resources>
如果您需要查看其他文件,请告诉我
【问题讨论】:
【参考方案1】:好的,问题已解决。 首先我的风格是错误的,我改变了:
<item name="background">@android:color/transparent</item>
到:
<item name="android:background">@android:color/transparent</item>
真正的问题是,BottomSheetDialog 的主题将应用程序的主题背景设置为默认背景,所以背景是黑色的。 通过添加解决:
<item name="android:background">@android:color/transparent</item>
在BottomSheetTheme中
【讨论】:
以上是关于膨胀BottomSheetDialog时替换片段的主要内容,如果未能解决你的问题,请参考以下文章