簡體   English   中英

如何在Android中創建自定義小部件

[英]How to create custom widget in android

我正在開發生日願望應用程序。 我想在手機的主屏幕上創建提醒朋友生日的設置提醒。 但是我想要像自定義小部件一樣的視圖。 誰能幫助我如何在android中動態創建小部件? 那就是使用編碼。

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);
        }
    }
}

這將是

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM