Getting default padding for AlertDialog

后端 未结 3 754
灰色年华
灰色年华 2021-02-02 05:40

I need to make a AlertDialog with a custom view. The message of a AlertDialog has a default padding but when i set a view it has no padding, i wand to

3条回答
  •  囚心锁ツ
    2021-02-02 06:08

    Just to add a more complete answer of how to use it:

    You can use in the layout XML file android:paddingStart="?dialogPreferredPadding" and android:paddingEnd="?dialogPreferredPadding" .

    Alternatively, if you want to do it by code:

    @JvmStatic
    fun getDimensionFromAttribute(context: Context, attr: Int): Int {
        val typedValue = TypedValue()
        return if (context.theme.resolveAttribute(attr, typedValue, true)) 
               TypedValue.complexToDimensionPixelSize(typedValue.data, context.resources.displayMetrics) 
               else 0
    }
    

    usage:

    val alertDialogPadding = getDimensionFromAttribute(context, R.attr.dialogPreferredPadding)
    view.setPadding(alertDialogPadding, 0, alertDialogPadding, 0)
    

    If you use a CheckBox (and maybe some other views), I don't think it's possible to set the paddings or margins so that it will get aligned nicely with other views in the dialog, unless you wrap it with a different view, or extend it

提交回复
热议问题