我在经过测试的项目page中创建了一个非常简单的Bottom Sheet
。首先,我创建了布局:
<?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:id="@+id/mainContent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingTop="24dp">
<Button
android:id="@+id/btn_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 1"
android:padding="16dp"
android:layout_margin="8dp"
android:textColor="@android:color/white"
android:background="@android:color/holo_green_dark"/>
</LinearLayout>
</ScrollView>
<android.support.v4.widget.NestedScrollView
android:id="@+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="350dp"
android:clipToPadding="true"
android:background="@android:color/holo_orange_light"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Bottom Sheets"
android:paddingTop="16dp"
android:textSize="16sp"/>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
在MainActivity中,我编写了以下代码:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private BottomSheetBehavior mBottomSheetBehavior;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View bottomSheet = findViewById(R.id.bottom_sheet);
Button btn1 = findViewById(R.id.btn_1);
btn1.setOnClickListener(this);
mBottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_1:
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
break;
}
}
}
但是当我在仿真器BottomSheetBehavior
上运行应用程序时,是EXPANDED
!!!当我将代码更改为此时:
switch (view.getId()) {
case R.id.btn_1:
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
break;
然后按btn_1 BottomSheetBehavior
不会折叠?问题是什么?
这是我的礼物:
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:28.0.0-alpha3'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
implementation 'com.android.support:design:28.0.0-alpha3'
}
答案 0 :(得分:1)
您必须将这两行添加到底部工作表布局文件中
app:behavior_peekHeight="0dp"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
layout_behavior-将底部工作表折叠和展开行为应用于上下布局 peekHeight-活动启动时要初始显示的布局高度,您可以根据视图高度进行更改
如果已经添加了它,那么可以请您共享包含底部工作表布局的主活动xml文件