如何用左/右/底部边距显示底部表格段



BottomSheetDialogFragment显示了全屏宽度,没有底部边距。我想设置左/右和底部边距。

我知道我们可以使用BottomSheetBehaviour并将其应用于布局中的View对象。但是我想使用BottomSheetDialogFragment

您可能不应该有底部边距(为什么不应该扩展到屏幕的底部?),但是您可以在布局设置后修改水平余量来设置水平余量由BottomSheetDialog。表加载一个名为design_bottom_sheet_dialog.xml的布局资源。如果打开该文件,则可以看到该表实际上将其加载到带有id=@+id/design_bottom_sheet的Framelayout中。由于设计资源已添加到您的应用中,因此您可以在代码中使用此库中的ID。

该策略简单地找到了Framelayout,获得其布局,并在那里更改边距。为此,子类底部表格fragment添加下面的覆盖。

override fun onActivityCreated(savedInstanceState: Bundle?) {
    super.onActivityCreated(savedInstanceState)
    val sheet: View? = dialog?.findViewById(R.id.design_bottom_sheet)
    val sheetLP = sheet?.layoutParams as? ViewGroup.MarginLayoutParams?
    sheetLP?.marginStart = this.resources.getDimensionPixelSize(R.dimen.bottomSheetHMargin) <-- your margin value dp
    sheetLP?.marginEnd = sheetLP!!.marginStart
}

注意,如果库更改了布局文件,则代码不会崩溃,但可能会停止工作。我已经在图书馆的1.2.1上进行了测试。

您可以拥有一个直接孩子的顶部。现在,在顶部布局中使用背景透明和填充,并将您的内容仅在布局中

<RelativeLayout 
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/parent_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/transparent"
            android:paddingLeft="20dp"
            android:paddingRight="20dp"
            android:paddingBottom="20dp">
   <FrameLayout>
      Content here
   </FrameLayout></RelativeLayout>

为我工作。

最新更新