我想使用anko而不用任何XML来警告带有圆角的警报。我在下面粘贴了当前尝试。有什么方法可以使警报的自定义视图完全透明,或者使其变为圆形并更改其背景颜色?
class TestActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
alert {
customView {
verticalLayout {
backgroundDrawable = GradientDrawable().apply {
shape = GradientDrawable.RECTANGLE
cornerRadius = 15f
setColor(Color.BLACK)
}
for(i in 0..5){
textView("Hello World") {
textColor = Color.WHITE
gravity = Gravity.CENTER
}.lparams(matchParent,dip(50))
}
}
}
}.show()
}
}
依赖项
ext.kotlin_version = '1.3.21'
ext.anko_version='0.10.8'
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0-alpha03'
implementation 'androidx.core:core-ktx:1.1.0-alpha05'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.2-alpha02'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0-alpha02'
// Anko Commons
implementation "org.jetbrains.anko:anko-commons:$anko_version"
// Anko Layouts
implementation "org.jetbrains.anko:anko-sdk15:$anko_version"
// sdk15, sdk19, sdk21, sdk23 are also available
implementation "org.jetbrains.anko:anko-appcompat-v7:$anko_version"
编辑:
var alertDialog: Dialog? = null
alertDialog = alert {
customView {
verticalLayout {
backgroundDrawable = GradientDrawable().apply {
shape = GradientDrawable.RECTANGLE
cornerRadius = 15f
setColor(Color.BLACK)
}
for(i in 0..5){
textView("Hello World") {
textColor = Color.WHITE
gravity = Gravity.CENTER
}.lparams(matchParent,dip(50))
}
button {
text = "Cancel"
setOnClickListener {
alertDialog?.dismiss()
}
}
}
}
}.show() as? Dialog
// This right here does the trick
alertDialog?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
alertDialog?.show()
做我想要的。谢谢,blastervla
答案 0 :(得分:1)
您可以执行以下操作:
val alertDialog: Dialog = alert {
customView {
/* Your custom view DSL */
}
}.build() as Dialog // You can use conditional as? syntax for extra safety
// This right here does the trick
alertDialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
alertDialog.show()
请注意,这样做,您需要在contentView内部自己设置标题和按钮,否则它们将显示在透明背景上。如果需要关闭按钮,可以在对话框声明中进行以下更改:
var alertDialog: Dialog? = null
alertDialog = alert {
customView {
/* ... */
button {
text = "Cancel"
setOnClickListener {
alertDialog?.dismiss()
}
}
}
}.build()
/* ... */