从BottomSheet顶部移除多余的空间

3

我在我的应用程序中添加了底部工作表。它运作良好,但唯一的问题是它覆盖的额外空间,我只想在底部工作表布局的顶部删除那些额外空间。

我想要删除高亮部分:

enter image description here

我如何去掉这些额外空间?

这是我的XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    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_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:id="@+id/coordinatorLayout"
    tools:context=".MainActivity">

    <include layout="@layout/content_main" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:orientation="horizontal"
        app:behavior_hideable="true"
        app:behavior_peekHeight="0dp"
        android:id="@+id/bottom_sheet"
        android:background="@android:color/white"
        app:layout_behavior="@string/bottom_sheet_behavior">

            <ImageView
                android:layout_weight="1"
                android:layout_margin="@dimen/five"
                android:padding="@dimen/ten"
                android:src="@drawable/ic_photo_camera"
                android:layout_width="@dimen/fifty"
                android:layout_height="@dimen/fifty" />

            <ImageView
                android:layout_weight="1"
                android:layout_margin="@dimen/five"
                android:padding="@dimen/ten"
                android:src="@drawable/ic_image_gallery"
                android:layout_width="@dimen/fifty"
                android:layout_height="@dimen/fifty" />

            <ImageView
                android:layout_weight="1"
                android:layout_margin="@dimen/five"
                android:padding="@dimen/ten"
                android:src="@drawable/ic_video_camera"
                android:layout_width="@dimen/fifty"
                android:layout_height="@dimen/fifty" />

            <ImageView
                android:layout_weight="1"
                android:layout_margin="@dimen/five"
                android:padding="@dimen/ten"
                android:src="@drawable/ic_video"
                android:layout_width="@dimen/fifty"
                android:layout_height="@dimen/fifty" />


    </LinearLayout>

</android.support.design.widget.CoordinatorLayout>

这是我的Java代码

public class MainActivity extends AppCompatActivity {


    BottomSheetBehavior behavior;
    View bottomSheet;
    CoordinatorLayout coordinatorLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        coordinatorLayout = (CoordinatorLayout) findViewById(R.id.coordinatorLayout);

        bottomSheet = findViewById(R.id.bottom_sheet);
        behavior = BottomSheetBehavior.from(bottomSheet);
        behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
            @Override
            public void onStateChanged(@NonNull View bottomSheet, int newState) {
                // React to state change
            }

            @Override
            public void onSlide(@NonNull View bottomSheet, float slideOffset) {
                // React to dragging events
            }
        });


        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
            }
        });
    }

//        Snackbar.make(coordinatorLayout,item + " is selected", Snackbar.LENGTH_LONG).setAction("Action", null).show();
//        behavior.setState(BottomSheetBehavior.STATE_COLLAPSED);


    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            if (behavior.getState()==BottomSheetBehavior.STATE_EXPANDED) {

                Rect outRect = new Rect();
                bottomSheet.getGlobalVisibleRect(outRect);

                if(!outRect.contains((int)ev.getRawX(), (int)ev.getRawY()))
                    behavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
            }
        }
        return super.dispatchTouchEvent(ev);
    }
}

1
从您的BottomSheet中删除顶部边距/填充,或给它一个负值。 - Phantômaxx
3个回答

2
我想我找到了答案。只需要从bottom_sheet即LinearLayout中删除android:fitsSystemWindows="true"。 以防万一,以下是xml代码。
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    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_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:id="@+id/coordinatorLayout"
    tools:context=".MainActivity">

    <include layout="@layout/content_main" />


    <LinearLayout android:layout_width="match_parent"
        android:layout_height="wrap_content"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:orientation="horizontal"
        app:behavior_hideable="true"
        app:behavior_peekHeight="0dp"
        android:id="@+id/bottom_sheet"
        android:background="@android:color/holo_orange_light"
        app:layout_behavior="@string/bottom_sheet_behavior" 
        xmlns:android="http://schemas.android.com/apk/res/android">

        <ImageView
            android:layout_weight="1"
            android:layout_margin="@dimen/five"
            android:padding="@dimen/ten"
            android:src="@drawable/ic_photo_camera"
            android:layout_width="@dimen/fifty"
            android:layout_height="@dimen/fifty" />

        <ImageView
            android:layout_weight="1"
            android:layout_margin="@dimen/five"
            android:padding="@dimen/ten"
            android:src="@drawable/ic_image_gallery"
            android:layout_width="@dimen/fifty"
            android:layout_height="@dimen/fifty" />

        <ImageView
            android:layout_weight="1"
            android:layout_margin="@dimen/five"
            android:padding="@dimen/ten"
            android:src="@drawable/ic_video_camera"
            android:layout_width="@dimen/fifty"
            android:layout_height="@dimen/fifty" />

        <ImageView
            android:layout_weight="1"
            android:layout_margin="@dimen/five"
            android:padding="@dimen/ten"
            android:src="@drawable/ic_video_camera"
            android:layout_width="@dimen/fifty"
            android:layout_height="@dimen/fifty" />


    </LinearLayout>
</android.support.design.widget.CoordinatorLayout>

为了方便我的使用,我已经使用了不同的可绘制对象,你需要将其改回原始的可绘制对象。

希望这有所帮助!


0

我曾经遇到过同样的问题。对我有用的方法是将 android:layout-behaviourLinearLayout 移动到 coordinatorLayout 中,或者干脆将其删除。


0
    android:fitsSystemWindows="false"

可以工作!


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