java - Android DialogPreference 宽度

标签 java android android-layout android-5.0-lollipop

我有一个自定义的 DialogPreference,它只包含一个日期选择器。一切正常,除了对话框有一个巨大的边框和标题,如下所示。

enter image description here

我希望对话框只是日期选择器:

enter image description here

我已经想出了如何通过将窗口标题设置为 null 来删除标题,但巨大的边框仍然存在。

我的自定义布局:

    <?xml version="1.0" encoding="utf-8"?>

    <LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <DatePicker 
        android:id="@+id/birthdayPicker"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_gravity="center_horizontal"
        android:calendarViewShown="false"/>

</LinearLayout>

我已经尝试将线性布局宽度/高度设置为 wrap_content,设置负边距,以及许多其他 hackkey 解决方案,但似乎没有任何效果。

到目前为止,我唯一的幸运是重写 onShow 并以编程方式将窗口大小设置为任意数量——比如 300dp 的宽度。这显然是 super duper hackey,我更喜欢更优雅的解决方案。任何帮助将不胜感激!

最佳答案

<?xml version="1.0" encoding="utf-8"?>

您的LinearLayout 不应是match_parent,而是wrap_content,例如DatePicker 的尺寸。

尝试:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <DatePicker 
        android:id="@+id/birthdayPicker"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_gravity="center_horizontal"
        android:calendarViewShown="false"/>

</LinearLayout>

关于java - Android DialogPreference 宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27907150/

相关文章:

java - webviews 打开默认浏览器

android - 是否可以使用 TwoLineListItem 填充 ListView?

javascript - 二叉表达式树的中缀

android - Kotlin 中按钮状态的不同背景

java - 在 Java 中将计数器重置为零?

android - 如何在 ArrayAdapter 的列表开头添加对象?

android - RecyclerView 未显示(正在显示)

android - 在不使用不必要的 Activity 的情况下构建 Android 测验

java - Wildfly 8 SEAM2 重定向 - javax.faces.context.PartialViewContextFactory

java - 未过期的谷歌日历api channel java