如何在android中创建自定义小部件

时间:2014-03-25 07:00:43

标签: android android-layout android-widget

我正在为生日祝福app工作。我想在手机主屏幕上创建我朋友生日的集提醒。但我希望它的视图像自定义小部件。任何人都可以帮助我动态地在android中创建小部件吗?那就是使用编码。

1 个答案:

答案 0 :(得分:1)

public class DynamicImageButton extends ImageView {

    public DynamicImageButton(final Context context, final AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) {
        final Drawable d = this.getDrawable();

        if (d != null) {
            // ceil not round - avoid thin vertical gaps along the left/right edges
        final int width = MeasureSpec.getSize(widthMeasureSpec);
        final int height = (int) Math.ceil(width * (float) d.getIntrinsicHeight() / d.getIntrinsicWidth()-10);
            this.setMeasuredDimension(width, height);
        } else {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }
}

This will be