我在经过测试的项目中创建了非常简单的Bottom Sheet,页面 .First是我创建的布局:
<?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'
}发布于 2018-06-25 07:17:33
您必须将这两行添加到底部工作表布局文件中。
app:behavior_peekHeight="0dp"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior"layout_behavior -要应用底部工作表折叠和展开行为来上下布局peekHeight --要在活动启动时显示初始布局的高度,可以根据视图高度进行更改。
如果已经添加了这些内容,那么请在包含底部工作表布局的地方共享您的主要活动xml文件。
https://stackoverflow.com/questions/51017935
复制相似问题