4

カスタムビューを描画してカスタム境界線を描画しようとしています。境界線の片側のサンプルを次に示します。

package com.sparkydev.guessaphrase;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RectShape;
import android.util.AttributeSet;
import android.view.View;

public class LeftBorder extends View {
    private ShapeDrawable vertRect, horizRect;

    public LeftBorder(Context context, AttributeSet attributeset) {
        super(context, attributeset);

        int width = this.getWidth();
        int height = this.getHeight();

        vertRect = new ShapeDrawable(new RectShape());
            vertRect.getPaint().setColor(Color.RED);
            vertRect.setBounds(0, 0, width/10, height);
        horizRect = new ShapeDrawable(new RectShape());
            horizRect.getPaint().setColor(Color.RED);
            horizRect.setBounds(0, 0, width, height/9);

    }

    protected void onDraw(Canvas canvas){
        vertRect.draw(canvas);
        horizRect.draw(canvas);
    }

}

そして反対側はほとんど同じように定義されます。XMLは次のように定義されています。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="@drawable/wallpaper">


    <!-- Declares left border's position, which is
    drawn programmatically.-->
    <com.sparkydev.guessaphrase.LeftBorder
        android:id="@+id/leftborder"
        android:layout_alignParentLeft="true"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />



        <!-- Used a FrameLayout to contain
        the menu buttons.-->
        <FrameLayout 
        android:id="@+id/FrameLayout01" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        >

            <!-- Menu buttons -->

        </FrameLayout>


    <!-- Declares right border position (on right
    of screen, below the left border) -->
    <com.sparkydev.guessaphrase.RightBorder
        android:layout_alignParentRight="true"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/leftborder"
    />



</RelativeLayout>

問題は、境界線がまったく表示されていないことです。背景は表示されますが、他には何も表示されません。

4

1 に答える 1

11

「カスタムコンポーネントの構築」開発ガイドから、カスタムビューを作成する場合、特に指定しない限り、サイズは100x100に設定されます。これがあなたが見ている問題と関係があるかどうかを確認してください。

「ほぼ確実にオーバーライドする必要があり、コンポーネントに何かを表示させたい場合はonMeasure()オーバーライドする必要があります。どちらにもデフォルトの動作がありますが、デフォルトでは何も実行されず、デフォルトでは常に100x100のサイズが設定されます。おそらくあなたが望むものではないでしょう。」onDraw()onDraw()onMeasure()

于 2010-09-26T22:02:32.000 に答える