Android底部页面布局边距

时间:2016-06-05 08:46:09

标签: android

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

我在片段底部有一个线性布局,现在我想从线性布局的顶部展开底部片材

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>

3 个答案:

答案 0 :(得分:3)

我有同样的问题它有点像黑客,但我让它像这样工作。

基本上只是制作了带有底部工作表的透明线性布局,然后在其中放置一个视图。然后给出了透明的线性布局填充底部。

<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>

答案 1 :(得分:0)

尝试使用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>

答案 2 :(得分: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);
    }

希望对您有所帮助。