安卓底部弹出式布局边距

12

如何在片段中设置底部表的边距?

我有一个线性布局位于片段底部,现在我想从该线性布局顶部展开底部表。

android:layout_marginBottom="36dp"不起作用,并且底部表覆盖了线性布局。

这是我的代码:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="36dp"
    android:background="@drawable/shadow"
    android:layout_gravity="bottom">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_add_white_24dp" />
</LinearLayout>

<LinearLayout
    android:id="@+id/bottom_sheet"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#ff0000"
    app:behavior_hideable="true"
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
    android:layout_marginBottom="36dp">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Test" />

</LinearLayout>

请参考以下链接:https://dev59.com/EVcP5IYBdhLWcg3wxM05 - Deepak Kumar
4个回答

4

关于底部边距我不确定,但是对于水平(起始或结束)边距,以下解决方案解决了我的问题:

首先在themes.xml(以前称为style.xml)中编写以下代码:

<style name="bottomSheetDialog" parent="Theme.Design.BottomSheetDialog">
    <item name="bottomSheetStyle">@style/bottomSheetDialogStyle</item>
</style>

<style name="bottomSheetDialogStyle" parent="Widget.Design.BottomSheet.Modal">
    <item name="android:layout_marginStart">8dp</item>
    <item name="android:layout_marginEnd">8dp</item>
</style>

在显示底部视图时设置样式:

DisplayOrderBottomSheet().apply {

    setStyle(DialogFragment.STYLE_NORMAL, R.style.bottomSheetDialog)

}.show(childFragmentManager)

4
我曾遇到同样的问题,这是一个小技巧,但我已经通过以下方式解决了它。
基本上只需创建一个带有底部表单的透明线性布局,然后在其中放置一个视图。随后,给透明线性布局添加 padding bottom 就可以解决问题。
<android.support.design.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:fitsSystemWindows="true"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="20dp"
>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:gravity="center"
    >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:transitionName="vatom"
        />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
    android:elevation="300dp"
    android:paddingBottom="20dp"
    android:paddingTop="20dp"
    >

    <View
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/colorAccent"
        />


</LinearLayout>


谢谢。这对我有用。我在使用NestedScrollView作为容器时尝试过添加padding,但由于滚动视图的match_parent实际上并没有填满整个屏幕,所以它并没有正确地工作。将其更改为线性布局对我有用。 - fobbymaster

1
尝试使用FrameLayout在根LinearLayout中容纳元素。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/paymentbottomsheet_view"
android:orientation="vertical"
android:background="#fff"
android:layout_width="match_parent"
android:layout_height="215dp"
app:behavior_hideable="true"
app:behavior_peekHeight="0dp"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
<FrameLayout
    android:paddingLeft="20dp"
    android:paddingRight="20dp"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent">
//Host another linear layout here to hold your elements.
</FrameLayout>
</LinearLayout>

0
你可以使用这段代码来设置你的底部弹出布局的边距:
你只需要更改这一行中的0.9,即lp.width = (displayMetrics.widthPixels * 0.9);,就可以改变你的边距大小。
        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
        lp.copyFrom(bottomSheetDialog.getWindow().getAttributes());

        DisplayMetrics displayMetrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

        lp.width = (displayMetrics.widthPixels * 0.9);
        bottomSheetDialog.getWindow().setAttributes(lp);
    }

希望这对你有所帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接