【问题标题】:Android DialogPreference widthAndroid DialogPreference 宽度
【发布时间】:2015-01-12 17:04:16
【问题描述】:

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

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

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

我的自定义布局:

    <?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、设置负边距以及许多其他 hackey 解决方案,但似乎没有任何效果。

到目前为止,我唯一的运气是覆盖 onShow 并以编程方式将窗口大小设置为任意数量 - 例如 300dp 的宽度。这显然是超级骗子,我更喜欢更优雅的解决方案。任何帮助将不胜感激!

【问题讨论】:

  • 为什么你的LinearLayout 的宽度和高度都有match_parent
  • 可能是 wrap_content 用于您的 LinearLayout layout_width
  • 我目前发现了同样的问题,使用 TimePicker 而不是 DatePicker。你有没有找到解决这个问题的方法?你介意分享onShow hack吗?

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


【解决方案1】:
<?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>

【讨论】:

  • 是的,我试过了,但没有运气 - match_parent vs wrap_content 似乎没有任何区别。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-14
  • 1970-01-01
  • 1970-01-01
  • 2014-09-05
  • 2017-07-22
相关资源
最近更新 更多