我尝试编码自定义的蓝色正方形组件,以便

  • 每当我们告诉我们要填充父级时,宽度或高度是父母的大小(例如,res/layout/main.xml)
  • 宽度或高度是30 PX(或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);
    }


}

这是我的主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()的事实:但是我错了。为什么 ?为什么将背景颜色设置为蓝色是不够的?

预先感谢有人可以帮助我。

有帮助吗?

解决方案

最后,每当用户设置为wrap_content时,我都会管理,我会以_ mast作为模式。

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



 }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top