【问题标题】:How can I get rid of rectangular TextView in AlertDialog?如何摆脱 AlertDialog 中的矩形 TextView?
【发布时间】:2021-12-28 14:48:38
【问题描述】:

所以我试图将警报对话框设置为一个圆圈,但我不能强制 TextView 是相同的形状。它总是有方形边框。 square outline/dialog phone。 我应该使用其他视图吗?还是我根本做不到? 我的XMLTextViewxml textview

<TextView
    android:id="@+id/expressionTextView"
    android:layout_width="300dp"
    android:layout_height="300dp"
    android:drawableTop="@drawable/correct_answer"
    android:drawableTint="@color/black"
    android:background="@drawable/oval_background"

    android:fontFamily="@font/roboto_light"
    android:gravity="center"
    android:paddingTop="110dp"
    android:paddingBottom="110dp"
    android:text="@string/youWin"
    android:elevation="30dp"

    android:textSize="40sp"
    android:layout_gravity="center" />

我正在尝试从片段启动警报对话框,所以这里是 Kotlin 代码:

 val dialogBinding = AlertDialogBinding.inflate(layoutInflater)
    val dialog = AlertDialog.Builder(activity).apply {
        setCancelable(true)
        setView(dialogBinding.root)
    }.create()
    dialog.show()

【问题讨论】:

  • 所有视图都是矩形的。但除非你在它们周围画出轮廓,否则没关系。只需确保您的内容适合圆圈,用户不会知道视图是方形的 - 他们将看到的只是圆圈上的文本。只需确保您的圆圈背景为圆圈外的部分使用透明颜色即可。

标签: android xml kotlin android-layout android-alertdialog


【解决方案1】:

这个白色部分与TextView无关,而是与对话窗口的背景有关; android 对话框的窗口与主应用程序窗口不同。

因此,要修复它,您需要使用window.setBackgroundDrawable() 为对话框窗口本身应用透明背景:

val dialogBinding = AlertDialogBinding.inflate(layoutInflater)
val dialog = AlertDialog.Builder(activity).apply {
    setCancelable(true)
    setView(dialogBinding.root)
}.create().apply {
        window?.setBackgroundDrawable(ResourcesCompat.getDrawable(resources, android.R.color.transparent, null))
    }
dialog.show()

注意:我假设您已经在 oval_background.xml 中使用了可绘制的透明背景;如果你的不工作,这是一个:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="#25C651" />
</shape>

【讨论】:

    猜你喜欢
    • 2016-11-09
    • 1970-01-01
    • 1970-01-01
    • 2021-11-12
    • 1970-01-01
    • 2021-08-26
    • 2017-06-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多