【问题标题】:BottomSheetDialogFragment header layout background transparent not workingBottomSheetDialogFragment 标题布局背景透明不起作用
【发布时间】:2020-03-06 20:31:08
【问题描述】:

我创建了一个 BottomSheetFragment。 一切都很好,除了底部表格的标题没有变得透明。有没有办法让背景透明,就像正常的 bottomsheetFragment 一样。 我尝试将父背景设置为透明,但它根本不起作用。

    <android.support.constraint.ConstraintLayout 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="wrap_content"
    android:id="@+id/layoutContainer"
    android:background="@android:color/transparent">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/immediateParent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <View
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
               >
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical"
                    >

                    <android.support.v4.widget.NestedScrollView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">
                    </android.support.v4.widget.NestedScrollView>
                </LinearLayout>
            </LinearLayout>
        </LinearLayout>
        <com.makeramen.roundedimageview.RoundedImageView xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/imageView"
            android:layout_width="@dimen/hundrad_twenty"
            android:layout_height="@dimen/hundrad_twenty"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="@dimen/sixty"
            app:riv_border_color="@color/transparent"
            app:riv_corner_radius="@dimen/twenty"
            app:riv_mutate_background="true"
            app:riv_oval="false" />
        <ImageView
            android:layout_width="@dimen/thirty"
            android:layout_height="@dimen/thirty"
            android:layout_alignParentRight="true"
            android:layout_marginTop="@dimen/hundred_fifty"
            android:layout_marginRight="100dp"/>
    </RelativeLayout>
</android.support.constraint.ConstraintLayout>

我已经厌倦了。

ConstraintLayout layoutContainer;
RelativeLayout immediateParent;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = null;
    view = inflater.inflate(R.layout.bottom_sheet_dialog, container);
    ButterKnife.bind(this, view);
    //parent layout transparent not working
    layoutContainer.setBackground(getResources().getDrawable(R.color.transparent));

    immediateParent.setBackground(getResources().getDrawable(R.color.transparent));
   //view transparent 
    view.setBackgroundColor(getResources().getColor(R.color.transparent));
    return view;
}

【问题讨论】:

  • “我尝试将父背景设置为透明,但它根本不起作用”您到底尝试了什么? (将其添加到您的帖子中)您是否尝试过使用 @android:color/transparent 作为背景颜色。
  • 是的,我做到了。我遵循@Max P 给出的方式。这使得底部工作表如预期的那样,但主要活动已变为白色。

标签: android xml android-fragments android-bottomsheetdialog


【解决方案1】:

您的视图实际上是透明的(当您显然使用透明而不是黑色时),问题是BottomSheetDialogFragment 将您的视图包装到它自己的非透明容器中。以下是使该容器透明的方法:https://stackoverflow.com/a/46469709/12976196

【讨论】:

  • 它在主页上方变成全白色背景,它应该像主页上方的底页一样是透明背景。
【解决方案2】:

在尝试了这么多方法后,我找到了解决方案。 只需在我的片段中添加此代码即可。

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    ((View) getView().getParent()).setBackgroundColor(Color.TRANSPARENT);
}

BottomSheetDialog with transparent background

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-18
    • 2023-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-14
    • 2021-10-13
    • 1970-01-01
    相关资源
    最近更新 更多