0

我试图编写一个自定义的蓝色方形组件,以便

  • 每当我们告诉在父布局中填充父级时,宽度或高度就是父级大小(比如说 res/layout/main.xml)
  • 每当我们告诉在父布局中包装内容时,宽度或高度都是 30 像素(或者 30 dpi 更好?)。

现在,感谢 rajesh.edi,我设法绘制了一个蓝色方块:但两种情况下的大小都是 30px * 30px(填充父项或包装内容)。那么有没有办法检测模式“包装内容”和模式“填充父”,以便我可以调整大小?

这是我对该组件的尝试(我称之为 BoardView,因为这个蓝色方块是制作棋盘组件之前的预备步骤):

package com.gmail.bernabe.laurent.android.simple_chess_board.views;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;

public class BoardView extends View {


    public BoardView(Context context) {
        super(context);
        setBackgroundColor(Color.BLUE);
    }



    public BoardView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }



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

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);

        int newWidth, newHeight;

        if (widthSpecMode == MeasureSpec.EXACTLY)
            newWidth = 30;
        else
            newWidth = MeasureSpec.getSize(widthMeasureSpec);

        if (heightSpecMode == MeasureSpec.EXACTLY)
            newHeight = 30;
        else
            newHeight = MeasureSpec.getSize(heightMeasureSpec);

        setMeasuredDimension(newWidth, newHeight);
    }

    @Override
protected void onDraw(Canvas canvas) {
    Rect rect = new Rect(0, 0, getWidth(), getHeight());
        Paint paint = new Paint();
        paint.setColor(Color.BLUE); 
        canvas.drawRect(rect, paint);
    }


}

这是我的 main.xml

<com.gmail.bernabe.laurent.android.simple_chess_board.views.BoardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/boardView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
/>

我确信错误不是来自我没有覆盖 View.onDraw() 的事实:但我错了。为什么 ?为什么将背景颜色设置为蓝色还不够?

如果有人可以帮助我,请提前感谢。

4

1 回答 1

0

最后我管理了每当用户设置为 wrap_content 时,我都会将 AT_MOST 作为模式。

 package com.gmail.bernabe.laurent.android.simple_chess_board.views;

 import android.content.Context;
 import android.graphics.Canvas;
 import android.graphics.Color;
 import android.graphics.Paint;
 import android.graphics.Rect;
 import android.util.AttributeSet;
 import android.view.View;

 public class BoardView extends View {


    public BoardView(Context context) {
        super(context);
    }



    public BoardView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }     



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



    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);

        int widthMeasure = MeasureSpec.getSize(widthMeasureSpec);
        int heightMeasure = MeasureSpec.getSize(heightMeasureSpec);

        int newWidth, newHeight;

        if (widthSpecMode == MeasureSpec.AT_MOST)
            newWidth = 30;
        else
            newWidth = widthMeasure;

        if (heightSpecMode == MeasureSpec.AT_MOST)
            newHeight = 30;
        else
            newHeight = heightMeasure;

        setMeasuredDimension(newWidth, newHeight);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        Rect rect = new Rect(0, 0, getWidth(), getHeight());
        Paint paint = new Paint();
        paint.setColor(Color.BLUE);
        canvas.drawRect(rect, paint);
    }



 }
于 2012-02-16T11:27:46.100 回答