开发者

Nine patch on a custom viewgroup

I made a nine-patch, and a custom viewgroup, then I made the background of that viewgroup to be the nine-patch.

The problem is: The nine-patch is ignoring the "content area" settings.

So: How I use a nine-patch properly in a custom view?

OR:

How I grab the content area from the ni开发者_开发技巧ne-patch so I can use it on the OnMeasure and OnLayout math?


Also use boolean getPadding(Rect padding) on the NinePatchDrawable to get the padding for the content (your content + 9patch padding = total group dize)


You may need to draw the object directly on the ViewGroup by overriding its onDraw method:

public void onDraw(Canvas canvas) {
 NinePatchDrawable bg =  (NinePatchDrawable) resources.getDrawable(id);
 if (bg != null) {
  bg.setBounds(0, 0, getWidth(), getHeight());
  bg.draw(canvas);
  }
 }


Get the background's padding in your onMeasure to decrease the available width (this is for laying out cells in a grid):

    @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    Drawable background = getBackground();
    Rect padding = new Rect();
    if (background != null && background.getPadding(padding));

    int width = MeasureSpec.getSize(widthMeasureSpec);
    int availableWidth = width - padding.left - padding.right;

And for onLayout, the padding is applied:

    @Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    int availableWidth = r - l - getPaddingLeft() - getPaddingRight();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜