【问题标题】:looking for Android dialog widget with rounded corners寻找带圆角的 Android 对话框小部件
【发布时间】:2019-07-28 15:12:26
【问题描述】:

我正在寻找像这个截图这样的 Android 对话框组件:

为白色背景上的白色对话框道歉,但希望您能看到漂亮的高程阴影、圆角,重要的是对话框右上角的关闭按钮。 (屏幕截图来自 Chrome 应用的无标签版)。

有没有人知道实现这一目标的库/sn-p 或类似的东西?谢谢。

编辑: 一些背景信息有望使问题更清楚:我有一个滚动活动,其中包含很多内容 - 文本和数据 - 我想通过可点击的文本/图像显示更多详细信息/图像。是的,我可以使用 AlertDialog,但我更喜欢屏幕截图中对话框窗口的外观。

【问题讨论】:

  • 使用CardView 作为圆角和高程对话框的背景。使用带有可点击背景的ImageView 作为关闭按钮。
  • 使用 CardView 作为 XML 文件中的布局包装器来获取阴影。
  • @CommonsWare:我没有意识到 CardView 如此灵活,这是一个很好的建议。我在文档中看到使用 Theme.Holo.Dialog 会将活动显示为对话框。我会试试这个。
  • @Ta Quang Tu:谢谢,我会尝试将它作为 DialogFragment 的包装器。

标签: android dialog


【解决方案1】:

你可以试试这个布局

<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"
xmlns:tools="http://schemas.android.com/tools"
tools:background="#B4CCCCCC"
android:orientation="vertical">

<androidx.cardview.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="10dp"
    app:cardCornerRadius="5dp"
    app:cardElevation="5dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">



    </RelativeLayout>

</androidx.cardview.widget.CardView>

【讨论】:

  • 感谢您的回复;但这似乎是一个永久的展示;我正在寻找一个可以由现有布局调用的对话框对象,显示对话框,然后在关闭时用户返回原始布局。我将编辑我的问题以使其更清楚。此外,我看到来自 Android Studio 的警告,其中包含以下代码:这个 RelativeLayout 视图是无用的(没有子项、没有背景、没有 id、没有样式)
【解决方案2】:

好吧,按照上面的建议,这是一个基本活动的粗略测试的结果,包含一个 CardView,主题为 Theme.Holo.Dialog:

并且使用 CardView 确实可以根据需要很好地显示圆角和高程,但是有一个大问题 - 在对话框的下方和下方看不到原始布局(调用 dailog 的地方),这个屏幕区域为空白。所以它不像一个对话。一个基于 DialogFragment 的自定义对话框是唯一的出路,这是我的下一个目标。

【讨论】:

    【解决方案3】:

    嗯,我失败了。这是我能想到的最好的:

    对话框现在就像一个对话框,放置在用户屏幕上方,并且 CardView 小部件(如上面所建议的)很好,但 DialogFragment 将它放置在一个白色矩形窗口中。不是我想要的。如果其他人有接受挑战的冲动,这里是我的布局 xml、对话框类和调用它的代码 sn-p:

    dialog_bigly.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="550dp">
    
        <androidx.cardview.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="10dp"
                app:cardCornerRadius="10dp"
                app:cardElevation="10dp"
                app:layout_constraintStart_toStartOf="parent"
                android:layout_marginStart="16dp"
                app:layout_constraintTop_toTopOf="parent"
                android:layout_marginTop="16dp"
                app:layout_constraintEnd_toEndOf="parent"
                android:layout_marginEnd="16dp"
                android:layout_marginBottom="16dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintHorizontal_bias="0.6">
            <LinearLayout
                    android:orientation="vertical"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">
                <TextView
                        android:text="  Dialog Title here"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" android:id="@+id/txtDialog_title"/>
                <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" app:srcCompat="@drawable/ic_do_not_disturb"
                        android:id="@+id/btnDialog_close" android:layout_gravity="right" android:clickable="true"/>
                <ImageView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent" app:srcCompat="@drawable/test_image207x344"
                        android:id="@+id/ivDialog_Image"/>
            </LinearLayout>
        </androidx.cardview.widget.CardView>
    </LinearLayout>
    

    DialogBig.kt

    package ...
    import ...
    
    class DialogBig: DialogFragment() {
        override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
    
            val builder = AlertDialog.Builder(activity!!)
            val inflater = activity!!.layoutInflater
            val dialogView = inflater.inflate(R.layout.dialog_bigly, null)
    
            val txtTitle = dialogView.findViewById<TextView>(R.id.txtDialog_title)
            val imgImage = dialogView.findViewById<ImageView>(R.id.ivDialog_Image)
            val btnClose = dialogView.findViewById<ImageView>(R.id.btnDialog_close)
    
            builder.setView(dialogView)
            btnClose.setOnClickListener { dismiss() }
            txtTitle.text = "dialog bigly title"
    
            return builder.create()
        }
    }
    

    最后是code sn-p

    btn_test.setOnClickListener {
        //create a new bigly dialog
        val dialog = DialogBig()
        dialog.show(supportFragmentManager, "testing")
    }
    

    【讨论】:

      【解决方案4】:

      我终于找到了解决方案(一个月后)。 @Bunny 建议让对话框片段的背景透明。 具体在DialogFragment类的onCreateView中,添加:

      dialog?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
      

      这可行,但它必须是一个'drawable'背景。但首先,结果——一个圆角的对话框:

      好的,代码如下。这是我的“rounded_corners.xml”形状:

      <?xml version="1.0" encoding="utf-8"?>
      <shape xmlns:android="http://schemas.android.com/apk/res/android">
       <corners android:radius="30dp" />
          <padding
              android:bottom="16dp"
              android:left="16dp"
              android:right="16dp"
              android:top="16dp" />
      </shape>
      

      在基本 CardView 小部件内的对话框片段布局 dialog_bigly.xml 中使用:

      <?xml version="1.0" encoding="utf-8"?>
      <androidx.cardview.widget.CardView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/base_card"
        android:layout_width="match_parent"
        android:layout_height="500dp"
        android:background="@drawable/rounded_corners"
        android:orientation="vertical"
        app:cardCornerRadius="20dp"
        app:cardElevation="10dp" >
      
        <androidx.constraintlayout.widget.ConstraintLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent">
      
          <TextView
              android:id="@+id/txtDialog_title"
              android:layout_width="0dp"
              android:layout_height="0dp"
              android:layout_marginStart="8dp"
              android:layout_marginBottom="2dp"
              android:fontFamily="@font/open_sans"
              android:text="Dialog Title here"
              app:layout_constraintBottom_toBottomOf="@+id/btnDialog_close"
              app:layout_constraintEnd_toStartOf="@+id/btnDialog_close"
              app:layout_constraintStart_toStartOf="parent"
              app:layout_constraintTop_toTopOf="parent" />
      
          <ImageView
              android:id="@+id/btnDialog_close"
              android:layout_width="24dp"
              android:layout_height="24dp"
              android:layout_marginEnd="8dp"
              style="@style/Widget.AppCompat.ActionButton"
              app:layout_constraintEnd_toEndOf="parent"
              app:layout_constraintTop_toTopOf="parent"
              app:srcCompat="@drawable/ic_red_close_24x24" />
      
          <ImageView
              android:id="@+id/ivDialog_Image"
              android:layout_width="0dp"
              android:layout_height="0dp"
              android:layout_marginTop="16dp"
              android:layout_marginBottom="16dp"
              app:layout_constraintDimensionRatio="w,1:1"
              app:layout_constraintTop_toBottomOf="@+id/txtDialog_title"
              app:layout_constraintStart_toStartOf="parent"
              app:layout_constraintEnd_toEndOf="parent"
              app:layout_constraintBottom_toBottomOf="parent"
              app:srcCompat="@drawable/test_image185x285" />
        </androidx.constraintlayout.widget.ConstraintLayout>
      </androidx.cardview.widget.CardView>
      

      最后是 DialogBig.kt kotlin 代码:

      package ...
      
      import ...
      
      class DialogBig: DialogFragment() {
        override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
      
          val builder = AlertDialog.Builder(activity!!)
          val inflater = activity!!.layoutInflater
          val dialogView = inflater.inflate(R.layout.dialog_bigly, null)
      
          val txtTitle = dialogView.findViewById<TextView>(R.id.txtDialog_title)
          val imgImage = dialogView.findViewById<ImageView>(R.id.ivDialog_Image)
          val btnClose = dialogView.findViewById<ImageView>(R.id.btnDialog_close)
      
          builder.setView(dialogView)
          btnClose.setOnClickListener { dismiss() }
          txtTitle.text = "my bigly dialog test title"
          return builder.create()
        }
      
        override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
          dialog?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
          return super.onCreateView(inflater, container, savedInstanceState)
        }
      }
      

      虽然我不太了解它是如何工作的,但我很高兴找到了解决方案。我认为问题的一部分是试图访问 DialogFragment 类深处的片段容器容器以应用透明度,而不仅仅是片段布局本身。

      【讨论】:

        猜你喜欢
        • 2016-07-18
        • 1970-01-01
        • 1970-01-01
        • 2013-05-27
        • 2012-04-11
        • 2019-08-14
        • 1970-01-01
        • 1970-01-01
        • 2018-11-30
        相关资源
        最近更新 更多