我试图将对话框设置为底部,但无法执行此操作。
它显示在页面顶部。

XML:

<?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"
    android:layout_width="match_parent"
    android:layout_height="370dp"
    android:background="@android:color/transparent"
    xmlns:tools="http://schemas.android.com/tools"
    android:padding="8dp"
    android:orientation="vertical">

    <android.support.v7.widget.CardView
        android:id="@+id/cv1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardCornerRadius="4dp"
        app:cardElevation="1dp"
        app:cardUseCompatPadding="true">

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent">


            <android.support.constraint.ConstraintLayout 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="wrap_content">


            <TextView
                android:id="@+id/textView14"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:background="@color/colorSecondaryBackground"
                android:fontFamily="@font/gotham_medium_regular"
                android:padding="8dp"
                android:text="Send Promotions"
                android:textColor="@color/colorSecondaryText"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

                <android.support.constraint.ConstraintLayout
                    android:id="@+id/promotionDataLayout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:paddingBottom="@dimen/two_margin"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@id/textView14">

                    <android.support.v7.widget.RecyclerView
                        android:id="@+id/promotionList"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        app:layout_constraintEnd_toEndOf="@id/guidelineEnd"
                        app:layout_constraintStart_toStartOf="@id/guidelineStart"
                        app:layout_constraintTop_toTopOf="parent">

                    </android.support.v7.widget.RecyclerView>

                    <android.support.constraint.Guideline
                        android:id="@+id/guidelineStart"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:orientation="vertical"
                        app:layout_constraintGuide_percent="0.08" />

                    <android.support.constraint.Guideline
                        android:id="@+id/guidelineEnd"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:orientation="vertical"
                        app:layout_constraintGuide_percent="0.92" />


                </android.support.constraint.ConstraintLayout>
                    </android.support.v7.widget.CardView>
                </android.support.constraint.ConstraintLayout
            </android.support.constraint.ConstraintLayout>
        </ScrollView>


    </android.support.v7.widget.CardView>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fabDone"
        android:layout_width="64dp"
        android:layout_height="64dp"
        android:src="@drawable/red_plus"
        app:layout_anchor="@+id/cv1"
        app:elevation="1dp"
        app:backgroundTint="@android:color/white"
        app:layout_anchorGravity="center|bottom" />
</android.support.design.widget.CoordinatorLayout>

我正在创建这样的对话框类:
class PromotionDialog:Dialog{

   var mContext:Context?=null
    constructor(context: Context) : super(context) {}

    constructor(context: Context, themeResId: Int) : super(context, themeResId) {
        mContext=context
    }

   override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContentView(R.layout.promotion_layout)// use your layout in place of this.

        window.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT));

    }

   }

解决方案尝试:
  • https://stackoverflow.com/a/9467151/1640009
  • https://stackoverflow.com/a/9467151/1640009

  • 由于设计限制,我无法使用bottomsheetdialog。

    编辑:
    我这样叫对话框
    val promotionDialog= PromotionDialog(this@DashboardActivity, android.R.style.Theme_Material_Light_NoActionBar_Fullscreen)
        promotionDialog.show()
    

    编辑2:

    正如@RAJA所建议的,这是因为FUll屏幕主题。

    所以现在我将主题更改为:
     <style name="ThemeDialog" parent="@style/Theme.AppCompat.Light.Dialog">
            <item name="android:windowBackground">@null</item>
            <item name="android:background">@android:color/transparent</item>
            <item name="android:windowMinWidthMajor">100%</item>
            <item name="android:windowMinWidthMinor">100%</item>
            <item name="android:windowIsFloating">true</item>
    
        </style>
    

    但底部仍有一定余量

    最佳答案

    您可以这样尝试:

     override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
    
            setContentView(R.layout.promotion_layout)// use your layout in place of this.
    
            window.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT));
            val windowlp = window.attributes
    
            windowlp.gravity = Gravity.BOTTOM
            window.attributes = windowlp
    
        }
    

    在 Activity 中访问对话框 View
    PromotionDialog(this).show();
    

    10-07 19:23
    查看更多