【发布时间】:2018-06-21 12:09:57
【问题描述】:
我想设置带有布局膨胀的底部表。意味着我想在创建布局后以编程方式添加它。
我尝试过但没有成功。写下面的代码,但底部的工作表没有出现。
View bottomSheet = getLayoutInflater().
inflate(R.layout.custom_layout_search_pickup_location, coordinatorMain, false);
// init the bottom sheet behavior
bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
// change the state of the bottom sheet
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
请记住,我已经知道 BottomSheetBehavior 现在正在使用 CoordinatorLayout 并且布局必须添加到 xml 文件中。
更新:我添加了底部工作表 xml 文件以进行说明。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center"
android:orientation="vertical">
<View
android:layout_width="48dp"
android:layout_height="2dp"
android:background="@color/light_grey">
</View>
<View
android:layout_width="48dp"
android:layout_height="2dp"
android:layout_marginTop="2dp"
android:background="@color/light_grey">
</View>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Where should we pick you up?"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
android:textStyle="bold" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_margin="10dp"
android:background="@drawable/bg_search_location">
<ImageView
android:id="@+id/imgViewPickUpMarker"
android:layout_width="22dp"
android:layout_height="22dp"
android:layout_centerVertical="true"
android:layout_marginStart="10dp"
app:srcCompat="@drawable/marker_pick_up" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginStart="10dp"
android:padding="10dp"
android:layout_toEndOf="@+id/imgViewPickUpMarker"
android:layout_toStartOf="@+id/imgViewPickUpMarkerSearch"
android:text="Search for pickup location" />
<ImageView
android:id="@+id/imgViewPickUpMarkerSearch"
android:layout_width="22dp"
android:layout_height="22dp"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginEnd="10dp"
android:layout_marginStart="10dp"
app:srcCompat="@drawable/places_ic_search" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_margin="10dp">
<ImageView
android:id="@+id/imgViewMap"
android:layout_width="22dp"
android:layout_height="22dp"
android:layout_centerVertical="true"
android:layout_marginStart="10dp"
android:tint="@color/translucent_black"
app:srcCompat="@android:drawable/ic_dialog_map" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginStart="10dp"
android:layout_toEndOf="@+id/imgViewMap"
android:text="SELECT VIA MAP"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
android:textColor="@color/translucent_black"
android:textStyle="bold" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginEnd="10dp"
android:layout_marginStart="20dp"
android:background="@color/ash">
</View>
【问题讨论】:
-
分享你的xml代码
-
您已经创建了底部工作表视图。您是否将其添加到主布局中,您的代码显示它只是膨胀而不是添加到视图中。您的 xml 也错过了必须添加到底部工作表布局(线性布局)的行为属性。我建议使用 BottomSheetFragment 而不是夸大视图。
标签: android material-design bottom-sheet