Comment créer un simple Affichage personnalisé?

Je voudrais créer un personnalisé View sur Android. J'ai essayé de faire aussi simple que possible et créé un presque vide de classe MyView et l'a utilisé dans mon LinearLayout mais l'application ne parvient pas à démarrer avec "Force Close". Comment puis-je faire une simple coutume View? Selon La Construction De Composants Personnalisés la View obtient la taille 100x100 si je ne la supplante pas onMeasure().

public class MyView extends View {

    public MyView(Context context) {
        super(context);
    }
}

Et je l'utilise dans un LinearLayout avec:

<view
    class="com.example.MyView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0.0" />

Ce que je fais mal?


Si j'utilise le constructeur qui itemon suggérer et de l'appel correspondant à la super-classe. Ensuite, la "Force Close" a disparu, mais mon LinearLayout est cassé, les composants après MyView n'est pas montré.

Voici mon main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    >
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0.0"
    android:background="#f00"
    android:text="Hello"
/>
<view
    class="com.example.MyView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0.0"
/>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0.0"
    android:background="#00f"
    android:text="World"
/>
</LinearLayout>
Vous pouvez consulter un bon exemple ici: http://www.sgoliver.net/blog/?p=1457
J'ai similaires besoins.. avez-vous obtenu ce que vous avez besoin . .partager du code plz

OriginalL'auteur Jonas | 2010-12-14