2014-04-01 11 views
15
package com.binod.customviewtest; 

import android.content.Context; 
import android.view.LayoutInflater; 
import android.widget.RelativeLayout; 

public class CustomView extends RelativeLayout{ 

    public CustomView(Context context) { 
     super(context); 
     // TODO Auto-generated constructor stub 

//  LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     LayoutInflater mInflater = LayoutInflater.from(context); 
     mInflater.inflate(R.layout.custom_view , this, true); 
    } 

} 

Einschließlich alsBenutzerdefinierte Ansicht Erweiterung Relative Layout-

<RelativeLayout 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" 
    tools:context=".MainActivity" > 

    <com.binod.customviewtest.CustomView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     ></com.binod.customviewtest.CustomView> 

</RelativeLayout> 

Benutzerdefinierte Ansicht als

<RelativeLayout 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" 
    > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/hello_world" /> 

</RelativeLayout> 

Gerade erst begonnen eine neue benutzerdefinierte Ansicht Hinzufügen und bekam einmal den Fehler Wenn ich diese dann löschen kann vorwärts bewegen

Ich bekomme Absturz "verursacht durch: android.view.InflateException: Binär XML-Dateizeile # 1: Fehler beim Aufblasen der Klasse "

+0

Haben Sie auch andere Konstrukteure und poste den Paketnamen für die benutzerdefinierte Ansicht und das XML, in dem Sie die benutzerdefinierte Ansicht haben – Raghunandan

+0

Stellen Sie die Datei custom_view.xml bereit –

+0

@RomanBlack Custom v iew ist in der Post hinzugefügt –

Antwort

35

Sie müssen 2 weitere Konstrukteure haben. Um zu wissen, warum

Do I need all three constructors for an Android custom view?

public class CustomView extends RelativeLayout{ 

    LayoutInflater mInflater; 
    public CustomView(Context context) { 
     super(context); 
     mInflater = LayoutInflater.from(context); 
     init(); 

    } 
    public CustomView(Context context, AttributeSet attrs, int defStyle) 
    { 
    super(context, attrs, defStyle); 
    mInflater = LayoutInflater.from(context); 
    init(); 
    } 
    public CustomView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    mInflater = LayoutInflater.from(context); 
    init(); 
    } 
    public void init() 
    { 
     View v = mInflater.inflate(R.layout.custom_view, this, true); 
     TextView tv = (TextView) v.findViewById(R.id.textView1); 
    tv.setText(" Custom RelativeLayout"); 
    } 
} 

ich ein Beispiel bin Entsendung. Mein Paket unterscheidet

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 
<com.example.testall.CustomView 
     android:id="@+id/timer1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     /> 
</RelativeLayout> 

custom_view.xml

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

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="60dp" 
     android:text="My Custom View" /> 

</RelativeLayout> 

MainActivity.java

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     } 
} 

Schnapp

enter image description here

Als pskink vorgeschlagen dort in einem RelativeLayout in activity_main.xml mit einem Kind CustomView. Anschließend erweitert CustomView RealtiveLayout, und Sie blenden dann erneut eine benutzerdefinierte Ansicht mit RelativeLayout und einer untergeordneten Textansicht auf. Keine Notwendigkeit für all diese. Nur eine benutzerdefinierte Ansicht. Haben Sie einen Textview programmatisch erstellt und fügen Sie dann Textview zu RelativeLayout

Edit:

activity_main.xml

<com.example.testall.CustomView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/timer1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    /> 

Custom

public class CustomView extends RelativeLayout{ 

    TextView tv; 
    public CustomView(Context context) { 
     super(context); 
     tv = new TextView(context); 
     init(); 

    } 
    public CustomView(Context context, AttributeSet attrs, int defStyle) 
    { 
    super(context, attrs, defStyle); 
    tv = new TextView(context); 
    init(); 
    } 
    public CustomView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    tv = new TextView(context); 
    init(); 
    } 
    public void init() 
    { 
     this.addView(tv); 
     tv.setText(" Custom RelativeLayout"); 
    } 
} 
+0

@Raghunandan und anstelle von einem RelativeLayout werden Sie am Ende mit * drei * RelativeLayouts – pskink

+0

@pskink oh richtig. – Raghunandan

+0

@pskink bearbeitet, aber ich wollte auf Konstruktoren hinweisen. Irgendwie hast du recht mit Relativlayouts und das ist unnötig. – Raghunandan

0

Versuchen Sie, Aktivität zu erhalten und nutzen diese

{ 
    LayoutInflater inflter = activity.getLayoutInflater(); 
    View v = inflter.inflate(R.layout.custom_view,null); 
    this.addView(v); or addView(v); 
    } 
+0

Ich glaube nicht, dass das das Problem ist. Der Aktivitätskontext wird an den Konstruktor übergeben. – Raghunandan

+0

Aktivität funktioniert nicht. Ich schließe diese in XML als

+0

@binod freundlich mehr schreiben Info. die XML-Datei, in der Sie die benutzerdefinierte Ansicht und den Paketnamen für die benutzerdefinierte Ansicht haben, sowie alle anderen Custruktoren, die Sie haben.Sie bieten sehr wenig Informationen, um Ihr Problem zu lösen – Raghunandan

Verwandte Themen