Question

Je suis en train de dessiner une bordure personnalisée en dessinant une vue personnalisée. Voici un échantillon d'un côté de la frontière:

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

}

Et l'autre côté est défini à peu près de la même façon. Le XML est défini comme tel:

<?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>

Le problème est, les frontières ne sont pas montrés du tout. Les spectacles de fond, mais rien d'autre.

Était-ce utile?

La solution

De le guide dev "Éléments de construction sur mesure" , lors de la création d'une vue personnalisée, il aura une taille de jeu de 100x100 jusqu'à indication contraire. Voyez si cela n'a rien à voir avec le problème que vous rencontrez.

  

"Vous presque certainement envie de passer outre onMeasure() et sont également susceptibles d'avoir besoin de remplacement onDraw() si vous voulez que le composant à quelque chose du spectacle. Bien que les deux ont un comportement par défaut, le onDraw() par défaut ne fait rien et le onMeasure() par défaut sera toujours mis une taille de 100x100 -. qui est sans doute pas ce que vous voulez "

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top