So erstellen Sie eine einfache benutzerdefinierte Ansicht?

Ich möchte erstellen Sie eine benutzerdefinierte View auf Android. Ich habe mich bemüht, es so einfach wie möglich und schuf eine fast leere Klasse MyView und es in meine LinearLayout aber die Anwendung schlägt fehl, starten Sie mit "Force Close". Wie kann ich eine einfache benutzerdefinierte View? Nach Erstellen Von Benutzerdefinierten Komponenten die View ruft die Größe 100x100 wenn ich nicht überschreiben onMeasure().

public class MyView extends View {

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

Und ich verwende es in einem LinearLayout mit:

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

Was mache ich falsch?


Wenn ich den Konstruktor verwenden, die itemon schlagen und den entsprechenden Aufruf der Superklasse. Dann die "Force Close" ist Weg, aber meine LinearLayout ist gebrochen, die Komponenten nach MyView nicht angezeigt.

Hier ist mein 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>
InformationsquelleAutor Jonas | 2010-12-14
Schreibe einen Kommentar