簡體   English   中英

如何在 android 中將頂部手柄添加到底部工作表?

[英]How to add top handle to bottom sheet in android?

頂部帶手柄的底板

我想實現這一目標。 任何人都可以幫我如何在底頁頂部添加這個句柄嗎?

試試這個 xml 底部欄的布局代碼:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layout_marginTop="10dp">
    <View
        android:layout_width="50dp"
        android:layout_height="3dp"
        android:background="@drawable/bg_bottom_sheet_view"
        android:alpha=".90"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_marginTop="10dp"
        android:orientation="vertical"
        android:paddingBottom="30dp"
        android:gravity="center"
        android:background="@drawable/bg_bottom_sheet">
        <!--Your Bottom Sheet views-->
    </LinearLayout>
</LinearLayout>

Java 參考代碼:

 public void showImagePickerBottomDialog(Context context){
        final BottomSheetDialog dialog = new BottomSheetDialog(context, R.style.BottomSheetMainStyle);
        dialog.setContentView(R.layout.dialog_bottom_sheet);
        dialog.show();
    }

bg_bottom_sheet_view.xml 可繪制

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="10dp"/>
    <solid android:color="#CCCCCC"/>
</shape>

bg_bottom_sheet.xml 可繪制

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:topLeftRadius="20dp"
        android:topRightRadius="20dp"/>
    <solid android:color="@android:color/white"/>
</shape>

在 theme.xml 添加BottomSheetMainStyle

<style name="BottomSheetMainStyle" parent="Theme.MaterialComponents.Light.BottomSheetDialog">
    <item name="bottomSheetStyle">@style/Widget.Test.BottomSheet.Modal</item>
</style>

<style name="Widget.Test.BottomSheet.Modal" parent="Widget.MaterialComponents.BottomSheet.Modal">
    <item name="backgroundTint">@android:color/transparent</item>
</style>

以上代碼的 Output 是:

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM