缩放视图会减小内容尺寸但增大空白边缘。

8
我正在尝试缩放一个包含日期选择器和时间选择器的水平线性布局。
android:scaleX="0.6"
android:scaleY="0.6"

问题在于缩放内容的同时增加了边距,因此我无法正确使用空间。在图片中,标记的白色区域是通过缩放获得的空间,但我无法使用这个空间(实际上,右侧的时钟被裁剪了)。
<LinearLayout
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:scaleX="0.6"
    android:scaleY="0.6">

    <DatePicker
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/datePicker"
        android:layout_gravity="center_horizontal"
        android:calendarViewShown="false"
        android:spinnersShown="false"
        android:layoutMode="opticalBounds"
        android:measureAllChildren="true" />

    <TimePicker
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/timePicker"
        android:layout_gravity="center_horizontal"
        android:layout_marginBottom="5dp" />
</LinearLayout>

有没有任何解决方案可以缩放视图而不出现空白的问题?

这张图片是在IDE预览中拍摄的吗? - asaf gitai
@asafgitai 是的,这来自于IDE预览。 - AndreaF
2
在这种情况下,问题可能仅与Holo pickers预览有关。您是否在真实设备上遇到此问题?如果是,使用Holo pickers时是否也会出现此问题?http://i.stack.imgur.com/TGYQ5.png - asaf gitai
如果你无法使用LinearLayout实现某些行为,请尝试使用RelativeLayout。 - Denis Kutlubaev
我会尝试所有的建议。谢谢。 - AndreaF
3个回答

2
我建议您不要使用xscale和yscale。相反,您可以为日期选择器和时间选择器提供权重。
<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_horizontal">

    <DatePicker
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="50"
        android:id="@+id/datePicker"
        android:layout_gravity="center_horizontal"
        android:calendarViewShown="false"
        android:spinnersShown="false"
        android:layoutMode="opticalBounds"
        android:measureAllChildren="true" />

    <TimePicker
       android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="50"
        android:id="@+id/timePicker"
        android:layout_gravity="center_horizontal"
        android:layout_marginBottom="5dp" />
</LinearLayout>

您可以在两侧添加填充或根据需要修改权重。


这个不行。如果DatePicker和TimePicker视图在设备屏幕上无法完全显示或重叠,那么它们将无法正常显示。 - starkej2
这太棒了!谢谢你! - Aleksandr

1

1

缩放不会改变您的控件大小,这意味着您的数据选择器在布局中占用的空间相同,无论是放大还是缩小。只有它们的外观会发生变化。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接