0

カスタムの青い四角のコンポーネントをコーディングしようとしました。

  • 親のレイアウト (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);
    }


}

ここに私の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 に答える