https://material.io/design/components/backdrop.html
我在 Material Design 上找到了这个,但是找不到任何资源。 考虑到它的布局,我认为它是由布局与材质构成,我试着使用布局 + 材质卡片视图我的活动文件。 这种方法制作背景布局正确吗?
此外,我还想知道我应该使用哪种布局。 RelativeLayout行不行? 求解答。
该组件(BackDrop)仍在Android Material Components
Android Material Components 开发中。
然而,如果您已经在使用 Material Components,那么实现您自己的并不难。见下文:
• CoordinatorLayout作为 root layout
• BottomSheetBehaviour
应用于 root layout的直接子元素
下面的示例使用了一个片段,我省略主机活动的详细信息,因为它与问题无关。 然而,你可以对一个活动做同样的事情。 你的 fragment layout file将看起来像下面..。
<?xml version="1.0" encoding="utf-8"?><androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="@+id/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">
<!--This the interface sitting behind the backdrop and shown when it is collapsed-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/colorPrimary"
android:padding="@dimen/activity_spacing">
<EditText
android:id="@+id/searchTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableStart="@drawable/ic_search_primary_xlight_24dp"
style="@style/EditTextStyle.Inverse.Large.Light"
android:hint="@string/search_hint"/>
<EditText
android:id="@+id/datesFilterButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableStart="@drawable/ic_calendar_primary_xlight_24dp"
style="@style/EditTextStyle.Inverse.Large.Light"
android:hint="@string/select_dates_hint"/>
</LinearLayout>
<!--This is the backdrop's content with a BottomSheetBehaviour applied to it-->
<LinearLayout
android:id="@+id/contentLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:behavior_peekHeight="56dp"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">
<!--This is the backdrop's header with a title and icon-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:clickable="true"
android:background="@drawable/ic_list_header_background"
android:padding="@dimen/activity_spacing"
android:elevation="4dp">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
style="@style/TextAppearance.Stems.Body2"
android:text="0 items(s)"/>
<ImageView
android:id="@+id/filterIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_filter_black_24dp"
android:layout_gravity="end"/>
</LinearLayout>
<!--And finally this is the body of the backdrop's content. You can add here whatever you need inside a view group (LinearLayout, RelativeLayout, SwipeRefreshLayout, ConstraintLayout, etc.)-->
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/swiperefresh"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorBackground">
<!--The content's body goes here-->
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</LinearLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
注意: 首先,位于 backdrop后的LinearLayout 使用 colorPrimary ,与Toolbar的背景颜色完全相同... 为了清晰起见,工具栏被省略了(记住,这个解决方案是为了一个fragment)。
然后fragment将看起来像这样..。
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
coordinatorLayout = (CoordinatorLayout)inflater.inflate(R.layout.fragment_hazards, container, false);
Context context = getContext();
if(context != null){
setTitle(context.getString(R.string.title_hazards));
}
filterIcon = coordinatorLayout.findViewById(R.id.filterIcon);
LinearLayout contentLayout = coordinatorLayout.findViewById(R.id.contentLayout);
sheetBehavior = BottomSheetBehavior.from(contentLayout);
sheetBehavior.setFitToContents(false);
sheetBehavior.setHideable(false);//prevents the boottom sheet from completely hiding off the screen
sheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);//initially state to fully expanded
filterIcon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
toggleFilters();
}
});
return coordinatorLayout;
}
private void toggleFilters(){
if(sheetBehavior.getState() == BottomSheetBehavior.STATE_EXPANDED){
sheetBehavior.setState(BottomSheetBehavior.STATE_HALF_EXPANDED);
}
else {
sheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
}
}
就是这样,你需要记住的唯一一件事就是root layout必须是CoordinatorLayout,然后 BottomSheetBehaviour 必须应用于 root layout的直接子元素