【问题标题】:Make DatePicker and TimePicker smaller使 DatePicker 和 TimePicker 更小
【发布时间】:2017-03-12 08:00:02
【问题描述】:

有没有办法在 Android 中制作更小的 DatePicker 和 TimePicker(但所有部分都可见)?我试图设置 layout_width="130dp" 但是 DatePicker 不可见,只是左上角。

【问题讨论】:

  • 你可以试试这个:andmobidev.blogspot.com/2010/01/…
  • 嘿,我不确定这对你有帮助,但我也发现原来的 TimePicker 很笨重,因此我自己做了一个 :) 查看我的 original question 了解更多信息.我制作的 TimePicker 大约是原来的一半大小 - 所以它肯定更小:)。
  • 检查我的解决方案(并希望接受它,因为这个页面是我的谷歌提要中的第一个结果,我花了几个小时来修复它)
  • 关注这个答案,它就像魅力一样! stackoverflow.com/a/34523787/1983018

标签: android


【解决方案1】:

使用属性如下:

以较小的 80% 原始示例

android:scaleY="0.8" 
android:scaleX="0.8"

【讨论】:

【解决方案2】:

(这实际上有效,而其他无效)经过大量搜索后,这有效:

<DatePicker
    android:id="@+id/dp_datepicker"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scaleX="0.7"
    android:scaleY="0.7"
    android:layout_marginLeft="-30dp"
    android:layout_marginRight="-30dp"
    android:datePickerMode="spinner"
    />

<TimePicker
    android:id="@+id/tp_timepicker"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scaleX="0.7"
    android:scaleY="0.7"
    android:layout_marginLeft="-30dp"
    android:layout_marginRight="-30dp"
    android:timePickerMode="spinner"
    />

android:scaleX="0.7" 缩小了可见选择器(它适用于DatePickerTimePicker),android:layout_marginLeft="-30dp" 允许缩小空间。

注意: 使用android:padding="-30dp" 的所有尝试均无效,使用android:layout_margin="-30dp"(未指定边)也无效。希望这可以帮助那些像我一样迷路的人

附: Google 修复了你该死的 DatePicker API!

【讨论】:

    【解决方案3】:

    不要使用“dp”作为布局宽度,而是使用重量百分比来使其更小。 要使 android:layout_weight 生效,请将 android:layout_width 设置为 0 dip。

    android:layout_height="wrap_content"

    android:layout_weight=".33"

    android:layout_width="0dip"

    android:textSize="12sp"

    【讨论】:

    • 实际上,当我给出答案时,它曾经有效。现在随着框架的发展,它没有。
    【解决方案4】:

    对我来说很有效,可以在小屏幕上以横向模式自定义布局在对话中适应日期和时间选择器。将容器(LinarLayout)的 weightsum 设置为 100,然后在 layout_weight 属性中使用加起来为 100 的小部件填充它。

        <LinearLayout
            android:orientation="horizontal" 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:weightSum="100"
            android:layout_gravity="center_horizontal">
    
                <DatePicker 
                    android:id="@+id/dlgDateTimePickerDate"
                    android:layout_width="wrap_content"
                    android:layout_weight="50"
                    android:layout_height="wrap_content" />
    
                <TimePicker
                    android:id="@+id/dlgDateTimePickerTime"
                    android:layout_weight="50"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
        </LinearLayout>
    

    【讨论】:

      【解决方案5】:

      你可以像这样简单地缩放:

      android:scaleY="0.5" android:scaleX="0.5"

      得到一半的大小。

      要获得更小的填充,android:padding="-20dp"

      从框架来看,选择器可能会与其他视图重叠,但在应用程序中不会。

      内边距和按钮都可以正常工作。

      【讨论】:

      • android:padding="-20dp" 对 TimePicker 不起作用
      【解决方案6】:

      另一种解决方案:

      把它放在LinearLayout 中并手动设置LinearLayout 的高度

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-19
        相关资源
        最近更新 更多