2014-02-10 4 views
6

Ich möchte dieses XML in Java in meiner Hauptaktivität übersetzen, die Ausgabe ist Navigationsschublade. Ich brauche nur Hilfe in diesem Teil, ich habe die restlichen Teile erledigt.So erstellen Sie DrawerLayout programmgesteuert

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/drawer_layout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context=".MainActivity" > 

<FrameLayout 
    android:id="@+id/main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 
</FrameLayout> 

<ListView 
    android:id="@+id/drawer" 
    android:layout_width="240dp" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    android:background="#FFF" 
    android:choiceMode="singleChoice"/> 

Ich möchte über XML in Java-Code schreiben, wie?

Vielen Dank im Voraus

aktualisieren

final DrawerLayout drawer = new android.support.v4.widget.DrawerLayout(this); 
    final FrameLayout fl = new FrameLayout(this); 
    fl.setId(CONTENT_VIEW_ID); 
    final ListView navList = new ListView (this); 

    drawer.addView(fl, new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); 
    drawer.addView(navList); 

    setContentView(drawer); 

Ich brauche etwas Hilfe Listenansicht des Layoutbreite und das Layout der Schwerkraft im Code anpassen.

+2

Was ist mit der Verwendung des XML-Layouts passiert? – gunar

+0

Ich möchte es in Code erstellen, ich bin nicht interessant in XML – user289175

Antwort

7

(.. Von der OP in Frage bearbeiten Beantwortet Umgerechnet auf ein Community Wiki Antwort Siehe Question with no answers, but issue solved in the comments (or extended in chat))

Die OP schrieb:

löste ich, hier ist der vollständige Code

final DrawerLayout drawer = new android.support.v4.widget.DrawerLayout(this); 
    final FrameLayout fl = new FrameLayout(this); 
    fl.setId(CONTENT_VIEW_ID); 
    final ListView navList = new ListView (this); 

    DrawerLayout.LayoutParams lp = new DrawerLayout.LayoutParams(
      100 , LinearLayout.LayoutParams.MATCH_PARENT); 

    lp.gravity=Gravity.START; 


    navList.setLayoutParams(lp); 

    drawer.addView(fl, new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); 
    drawer.addView(navList); 

    setContentView(drawer);