问题描述
我在应用程序中添加了bottomsheet.ii工作正常,但是唯一的问题是它覆盖了多余的空间,我只想删除Bottomsheet布局顶部的那些多余空间.
I added bottomsheet in my application.ii work good but only problem is extra space it cover and I just want to remove those extra spaces on the top of Bottomsheet layout.
我要删除突出显示的部分:
I want to remove the highlighted part:
我如何删除那些多余的空格?
How I can remove those extra spaces?
这是我的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);
}
}
推荐答案
我认为我已经找到答案了.只需从底部工作表中删除 android:fitsSystemWindows ="true"
,即LinearLayout.以防万一我将xml代码放在下面.
I think I have got the answer. Just need to remove the android:fitsSystemWindows="true"
from bottom_sheet i.e the LinearLayout.Just in case I am putting the xml code below.
<?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>
仅供我使用,我使用了其他可绘制对象,您需要将其更改回原始对象.
Just for my use, I have used different drawable you need to change it back to the original one.
希望有帮助!
这篇关于删除BottomSheet顶部的多余空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!