簡體   English   中英

如何在BottomSheetDialog android的頂部中心添加關閉按鈕?

[英]how to add close button on the top-center of BottomSheetDialog android?

想要在BottomSheetDialog上添加關閉按鈕來關閉它。

我正在使用BottomSheetDialog的自定義視圖(Linearlayout)。

filterDialog = new BottomSheetDialog(context); filterDialog.setContentView(rootView); filterDialog.getWindow().setWindowAnimations(R.style.CustomDialogAnimation); filterDialog.show();

下圖是我的filterDialog:

在此輸入圖像描述

這就是我想要實現的目標:

在此輸入圖像描述

任何幫助將不勝感激。

我通過使用自定義(底部)動畫的Dialog(全屏)解決了這個問題。

設計:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorTransparent"
android:orientation="vertical"
>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    >
    <ImageView
        android:id="@+id/img_close"
        android:layout_width="@dimen/fifty_dp"
        android:layout_height="@dimen/thirty_dp"
        android:src="@drawable/ic_close_filter"/>
</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:orientation="vertical">


<!-- All your required widgets here-->
    </LinearLayout> </LinearLayout>

Java代碼:

  Dialog filterDialog;
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    // Get the layout inflater
    LayoutInflater inflater = getActivity().getLayoutInflater();
    View rootView = inflater.inflate(R.layout.dialog_filter, null);

    final ImageView close = (ImageView) rootView.findViewById(R.id.img_close);

    builder.setView(rootView);

    filterDialog = builder.create();

    WindowManager.LayoutParams lp2 = new WindowManager.LayoutParams();
    Window window = filterDialog.getWindow();
    filterDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
    lp2.copyFrom(window.getAttributes());
    //This makes the dialog take up the full width
    lp2.width = ViewGroup.LayoutParams.MATCH_PARENT;
    lp2.height = ViewGroup.LayoutParams.WRAP_CONTENT;
    window.setAttributes(lp2);
    Window dialogWindow = filterDialog.getWindow();
    WindowManager.LayoutParams lp = dialogWindow.getAttributes();
    dialogWindow.setGravity(Gravity.BOTTOM );

    filterDialog.getWindow().setWindowAnimations(R.style.CustomDialogAnimation);
    filterDialog.show();

你可以這樣做 -

<!--you bottom sheet linear layout -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <!--Layout contains button-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:background="@android:color/transparent"
            android:clickable="false"   
            >

            <!--Your button-->
            <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" 
                android:src="your drawable"
                android:background="@android:color/transparent"
                android:clickable="true"/>
        </LinearLayout>

        <!--put Your other views in below linear layout -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        </LinearLayout>
    </LinearLayout>

注意:可點擊的假是非常重要的一點。 這樣做可以訪問線性布局后面的視圖。 同時將背景顏色設置為透明

暫無
暫無

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

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