I've tried to code a custom blue square Component so that

  • width or height is the parent size whenever we tell to fill parent, in the parent layout (let's say res/layout/main.xml)
  • width or height is 30 px (or is 30 dpi better ?) whenever we tell to wrap content in the parent layout.

For now, thanks to rajesh.edi, I managed to paint a blue square : but the size is 30px * 30px in both cases (Fill parent or wrap content). So is there a way to detect the mode "wrap content" and the mode "fill parent", so that I can adapt the size ?

Here is my attempt for the component (I called it BoardView, because this blue square is a preleminary step before making a Chess Board component) :

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


}

and here is my 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"
/>

I was sure that the error didn't come from the fact I did not override View.onDraw() : but I was wrong. Why ? Why setting background color to blue is not enough ?

Thanks in advance if someone can help me.

有帮助吗?

解决方案

Finally I managed Whenever the user set to wrap_content, I get AT_MOST as mode.

 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