自定義帶描述的ImageView

2022-04-21 10:29:07 字數 1717 閱讀 6657

在values/attrs.xml檔案中建立自定義的屬性。設定了自定義的屬性。

布局檔案

3.自定義view,在view的構造方法中獲取到自定義的屬性

private string texttitle; // 下的字型

private bitmap image;

private int imagescale;

private int textcolor;

private int textsize;

private rect rect; // 整個控制項的邊界

private paint paint;

private rect textbound; // 文字的邊界

// 控制項的寬高

private int width;

private int height;

private static final int image_scale_fitxy = 0;

private static final int image_scale_center = 1;

public mycustomimageview(context context)

public mycustomimageview(context context, attributeset attrs)

public mycustomimageview(context context, attributeset attrs, int defstyleattr)

}typedarray.recycle();

rect = new rect();

textbound = new rect();

paint = new paint();

// 設定字型的大小

paint.settextsize(textsize);

// 設定字型需要的大小範圍

log.e("tag", texttitle + "texttitle");

log.e("tag", textbound + "textbound");

paint.gettextbounds(texttitle, 0, texttitle.length(), textbound);

}4.重寫onmeasure()方法

@override

protected void onmeasure(int widthmeasurespec, int heightmeasurespec) else

// 設定高度

int heightmode = measurespec.getmode(heightmeasurespec);

int heightsize = measurespec.getsize(heightmeasurespec);

if (heightmode == measurespec.exactly) else

// 設定測量的尺寸

setmeasureddimension(width, height);

}5.重寫ondraw()方法

@override

protected void ondraw(canvas canvas) else

rect.bottom -= textbound.height();

if (imagescale == image_scale_fitxy) else

}樣式圖如下:

padding的圖示解析:

帶自定義title的tabhost

step1 搭架子 tab ayout.xml 容器 tab item.xml 選項卡內容 title.xml title內容 step2 主要 生成view控制activity跳轉 package com.hustma.tablehost.activity import android.conte...

自定義帶圓角的ImageView

最近有乙個實現乙個帶有圓角的imageview的需求,在網上找了找三方,雖然demo都是正確的,但是移植過來就不可以了,因為請求鏈結的時候用的是xutils中bitmap來進行解析的,這樣就總是會報型別轉換異常的錯誤.就這樣只能自己定義乙個了.demo package com.yizooo.yizo...

Android 自定義帶圓角的dialog

由於感覺android系統自帶的dialog樣式不是很美觀,就自己定義了乙個dialog 1.在styles裡定義樣式 2.在drawable裡定義乙個shape畫圓角 3.在layout裡定義個性化布局 4.activity裡的 layoutinflater inflater getlayouti...