寻找带有圆角的Android对话框小部件
我正在寻找一个Android对话框组件,如下图所示: 对于白色背景上的白色对话框表示歉意,但希望您能看到漂亮的立面阴影、圆角,以及对话框右上角的“关闭”按钮。 (截图取自Chrome应用程序的无标签版本) 有人知道实现这一点的库/代码段或类似的东西吗?谢谢 编辑:寻找带有圆角的Android对话框小部件,android,dialog,Android,Dialog,我正在寻找一个Android对话框组件,如下图所示: 对于白色背景上的白色对话框表示歉意,但希望您能看到漂亮的立面阴影、圆角,以及对话框右上角的“关闭”按钮。 (截图取自Chrome应用程序的无标签版本) 有人知道实现这一点的库/代码段或类似的东西吗?谢谢 编辑: 一些背景信息,希望能让问题更清楚:我有一个滚动活动,其中有很多内容-文本和数据-我想通过可点击的文本/图像显示更多细节/图像。是的,我可以使用AlertDialog,但我更喜欢屏幕截图中对话框窗口的外观。您可以尝试这种布局 <
一些背景信息,希望能让问题更清楚:我有一个滚动活动,其中有很多内容-文本和数据-我想通过可点击的文本/图像显示更多细节/图像。是的,我可以使用AlertDialog,但我更喜欢屏幕截图中对话框窗口的外观。您可以尝试这种布局
<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>
根据上述建议,这是对一项基本活动的粗略测试结果,该活动包含一个带有Theme.Holo.Dialog主题的CardView:
使用CardView确实可以根据需要很好地显示圆角和立面,但存在一个大问题——对话框下方和下方看不到原始布局(调用dailog的位置),屏幕的这个区域是空白的。所以它不像一个对话。基于DialogFragment的自定义对话框是唯一的出路,这是我的下一个目标。嗯,我失败了。这是我能想到的最好办法: 该对话框现在就像一个对话框,放置在用户屏幕上方,CardView小部件(如上所述)很好,但DialogFragment将其放置在一个白色矩形窗口中。不是我想要的。如果其他人有接受挑战的冲动,下面是我的布局xml、对话框类和调用它的代码段: 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>
我终于找到了解决办法(一个月后)@Bunny建议将对话框片段的背景设置为透明。 具体而言,在DialogFragment类的onCreateView中,添加:
dialog?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
这是可行的,但有一个限制,即它必须是一个“可绘制”的背景。但首先,结果是一个圆角对话框:
好的,代码如下。这是我的“圆角.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>
最后是对话框big.ktkotlin代码:
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)
}
}
包。。。
进口
类DialogBig:DialogFragment(){
重写FunonCreateDialog(savedInstanceState:Bundle?:对话框){
val builder=AlertDialog.builder(活动!!)
val充气机=活动!!.LAYOUTING充气机
val dialogView=充气机。充气(R.layout.dialog\u bigly,null)
val txtTitle=dialogView.findViewById(R.id.txtDialog\u标题)
val imgImage=dialogView.findviewbyd(R.id.ivDialog\u图像)
val btnClose=dialogView.findViewById(R.id.btnDialog\u close)
builder.setView(dialogView)
btnClose.setOnClickListener{disease()}
txtTitle.text=“我的大对话框测试标题”
returnbuilder.create()
}
覆盖创建视图(充气机:布局充气机,容器:ViewGroup?,savedInstanceState:Bundle?):视图{
dialog?.window?.setBackgroundDrawable(可彩色绘制(彩色.透明))
返回super.onCreateView(充气机、容器、savedInstanceState)
}
}
虽然我真的不明白这是怎么回事,但我很高兴找到了解决办法。我认为问题的一部分是试图访问DialogFragment类深处的碎片持有者容器以应用透明度,而不仅仅是碎片布局本身。使用
CardView
作为对话框的背景,用于圆角和立面。使用带有可单击背景的ImageView
作为关闭按钮。可以使用CardView
作为XML文件中的布局包装来获得阴影。@Commonware:我没有意识到CardView如此灵活,这是一个很好的建议。我在文档中看到,使用Theme.Holo.Dialog将活动显示为对话框。我要试试这个。@Ta Quang Tu:谢谢,我要试着把它包装在DialogFragment上。谢谢你的回复;但这似乎是一种永久性的展示;我正在寻找一个对话框对象,它可以被现有布局调用,显示对话框,然后当关闭时,用户返回到原始布局。我将修改我的问题,使之更清楚。此外,我还看到了来自Android Studio的警告:这个RelativeLayout视图是无用的(没有孩子,没有背景,没有id,没有样式)
<?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>
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)
}
}