Resizing/Scaling Down Android Widgets (DatePicker and TimePicker)

大憨熊 提交于 2019-12-09 05:09:15

问题


I'm not sure if this is possible, and I couldn't find a topic based on it, but if it's been answered before drop me a link and that will be that.

What I'm looking to do right now is resize some of the default Android widgets, specifically DatePicker and TimePicker, to use in an Activity. But as far as I can see the only result of modifying the width or height of either Picker (in a negative direction) results in a cropped view, rather than a scaled/stretched view of the widget.

I am open to my own custom widgets of my own, but I would really prefer to keep this project as simple and clean as possible, matching the Android OS UI as much as possible, so using the native DatePicker and TimePicker seems like a logical choice to me. If anyone knows how to scale these widgets down rather than cropping them, I'd really appreciate it.

Thanks.


回答1:


It is a very bad hack, but it should work: Create a new view extending LinearLayout, overwrite method getChildStaticTransformation and setStaticTransformationsEnabled explicit to true.

In the method getChildStaticTransformation you can manipulate the tranformation parameter to scale down all the content of your extended LinearLayout.

And then add the DatePicker or something else as a child of this view.

EG:

 public class ZoomView
    extends LinearLayout
{

private float sf = 1f;

public ZoomView(final Context context, final AttributeSet attrs)
{
    super(context, attrs);
    setStaticTransformationsEnabled(true);
}

public ZoomView(final Context context)
{
    super(context);
    setStaticTransformationsEnabled(true);
}

public void setScaling(final float sf)
{
    this.sf = sf;
}

@Override
protected boolean getChildStaticTransformation(final View child, final Transformation t)
{
    t.clear();
    t.setTransformationType(Transformation.TYPE_MATRIX);
    final Matrix m = t.getMatrix();
    m.setScale(this.sf, this.sf);
    return true;
}

}


来源:https://stackoverflow.com/questions/4261527/resizing-scaling-down-android-widgets-datepicker-and-timepicker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!