【问题标题】:How to create custom widget in android如何在android中创建自定义小部件
【发布时间】:2014-03-25 07:00:43
【问题描述】:

我正在开发生日祝福应用程序。我想在手机的主屏幕上设置我朋友生日的提醒。但我想要像自定义小部件一样查看它。谁能帮我了解如何在android中动态创建小部件?那就是使用编码。

【问题讨论】:

标签: android android-layout android-widget


【解决方案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);
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-06
    相关资源
    最近更新 更多