2010-03-09 16 views
34

ok,benutzerdefinierte Ansicht mit Layout

was ich versuche, eine benutzerdefinierte Ansicht in das Standard-Layout main.xml zu tun ist, einzubetten:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <com.lam.customview.CustomDisplayView 
     android:id="@+id/custom_display_view1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" /> 


    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 

     <Button 
      android:id="@+id/prev" 
      android:layout_width="0dip" 
      android:layout_height="wrap_content" 
      android:layout_weight="50" 
      android:textAppearance="?android:attr/textAppearanceSmall" 
      android:text="@string/prev" /> 
    </LinearLayout> 
</LinearLayout> 

, wie Sie die Klasse com genannt zu sehen ist. lam.customview.CustomDisplayView mit der ID custom_display_view1.

jetzt in der com.lam.customview.CustomDisplayView Klasse, ich möchte ein anderes Layout namens custom_display_view.xml verwenden, weil ich nicht programmgesteuert Steuerelemente/Widgets erstellen möchte.

custom_display_view.xml ist nur eine Taste und ein Bild, dessen Inhalt ich auf Grundlage bestimmter Bedingungen ändern wollen:

1)

public CustomDisplayView(Context context, AttributeSet attrs) { 
    super(context, attrs); 

    try 
    { 
     // register our interest in hearing about changes to our surface 
     SurfaceHolder holder = getHolder(); 
     holder.addCallback(this); 

     View.inflate(context, R.layout.custom_display_view, null); 
:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
    <TextView 
    android:id="@+id/display_text_view1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello" 
    /> 
    <ImageView 
    android:id="@+id/display_image_view1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 
    </ImageView> 

</LinearLayout> 

ich zu tun versucht

...

, aber habe diesen Fehler, "03-08 20: 33: 15.711: Fehler/onCreate (10879): Binär XML-Datei Zeile # 8: Fehler beim Aufblasen der Klasse java.lang.reflect.Constructor ".

2)

public CustomDisplayView(Context context, AttributeSet attrs) { 
    super(context, attrs); 

    try 
    { 
     // register our interest in hearing about changes to our surface 
     SurfaceHolder holder = getHolder(); 
     holder.addCallback(this); 

     View.inflate(context, R.id.custom_display_view1, null); 

...

aber habe diesen Fehler, „03-08 20: 28: 47,401: ERROR/CustomDisplayView (10806): Ressourcen ID # 0x7f050002 Typ # 0x12 ist nicht gültig "

auch, wenn ich es auf diese Weise zu tun, wie jemand vorgeschlagen hat, es mir nicht klar ist, wie th e custom_display_view.xml ist der benutzerdefinierten Ansichtsklasse zugeordnet.

danke.

+0

Sprache? Rahmen? –

+0

hi ich habe das gleiche Problem ... hast du irgendeine Lösung ??? – Ads

Antwort

0

Versuchen Sie es mit

context.getLayoutInflater().inflate(R.id.custom_display_view1, null); 
7

Sie können die cutom_display_view in Ihrer benutzerdefinierten Klasse aufblasen durch:

public CustomDisplayView(Context context, AttributeSet attrs) { 
    super(context, attrs);   
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      if(inflater != null){  
       inflater.inflate(R.layout.custom_display_view, this); 
      } 
} 
+12

Das ist viel sauberer: View.inflate (Kontext, R.layout.myactionbar, this); – Ian

70

hatte ich genau das gleiche Problem, und das Problem war, dass ich die falsche ID wurde mit. .. und es sieht aus wie du bist auch.

sollten Sie

R.layout.custom_display_view

-not-

R.id.custom_display_view

+2

Perfekte Antwort, ich hatte fälschlicherweise das gleiche Problem und das hat wirklich geholfen –

0

Linie # 8 ist Ihre benutzerdefinierte Ansicht in der ersten Layout werden verweisen. Versuchen Sie, das falsche Layout zu laden, also versucht es sich rekursiv zu laden?(Ich habe gerade die gleiche Sache)

auch Sie haben:

R.layout.custom_display_view 

gegen

R.id.custom_display_view1 

mit dem am Ende dort ... welches ist es?

4

Wenn Sie Ihr Layout aufblasen, sollten Sie einen Verweis auf die View-Instanz übergeben, um sie als Root zu identifizieren. Anstatt also Aufruf:

View.inflate(context, R.layout.custom_display_view, null); 

Call:

View.inflate(context, R.layout.custom_display_view, this); 

See: The docs

+0

Können Sie mehr Code zur Verfügung stellen, um zu zeigen, wie das funktioniert? –

+0

@Frank, der Code ist ähnlich dem des OP. –

+0

Dies funktioniert nur, wenn die benutzerdefinierte Klasse eine Viewgroup ist – smac89

7

Dieser Blog-Eintrag hat mir geholfen, zu verstehen, was http://trickyandroid.com/protip-inflating-layout-for-your-custom-view/ immens zu tun. Für den Fall, hier der Blog-Post verschwindet, sind einige Teile des Codes:

public class Card extends RelativeLayout { 
    public Card(Context context) { 
     super(context); 
     init(); 
    } 

    public Card(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     init(); 
    } 

    public Card(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     init(); 
    } 

    private void init() { 
     inflate(getContext(), R.layout.card, this); 
    } 
} 

mit diesem Layout:

<?xml version="1.0" encoding="utf-8"?> 

<merge xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:padding="@dimen/card_padding" 
    android:background="@color/card_background"> 

    <ImageView 
     ... /> 

    <TextView 
     ... /> 

</merge> 

Die Steuerung enthalten ist, wie:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin"> 

    <com.trickyandroid.customview.app.view.Card 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@color/card_background" 
     android:padding="@dimen/card_padding"/> 

</FrameLayout> 

Wo com.trickyandroid .customview.app.view ist der Namensraum der Klassenkarte. Eine Sache, die für mich neu war, war das "merge" -Tag, das am Ende derselbe Knoten ist wie das Card-Tag im enthaltenden Dokument.

Verwandte Themen