2014-02-10 163 views
6

我想在我的主要活動中將此xml轉換爲java,輸出爲導航抽屜。我只需要在這部分幫助,我完成了其餘部分。如何以編程方式創建DrawerLayout

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

我想上面寫Java代碼的XML,如何?

在此先感謝

更新

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); 

我需要一些幫助來定製列表視圖的佈局寬度和佈局重力代碼。

+2

XML佈局使用情況如何? – gunar

+0

我想在代碼中創建它,我在xml中不感興趣 – user289175

回答

7

(可以通過在一個問題編輯OP回答轉換爲一個社區維基答案見Question with no answers, but issue solved in the comments (or extended in chat)

的OP寫道:

我解決了,這裏是完整的代碼

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); 
相關問題