【问题标题】:Removing the extra space from Top of BottomSheet从 BottomSheet 顶部删除多余的空间
【发布时间】:2018-06-09 09:06:20
【问题描述】:

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

我要删除高亮部分:

如何删除这些多余的空格?

这是我的 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);
    }
}

【问题讨论】:

  • 从您的 BottomSheet 中删除上边距/填充或给它一个负值。

标签: android android-layout


【解决方案1】:

我想我已经得到了答案。只需从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>

仅供我使用,我使用了不同的drawable,你需要将它改回原来的。

希望对你有帮助!

【讨论】:

    【解决方案2】:
        android:fitsSystemWindows="false"
    

    会起作用的!

    【讨论】:

      猜你喜欢
      • 2016-07-23
      • 2016-12-28
      • 1970-01-01
      • 2017-10-25
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多