7

我正在編寫一個Android應用程序以利用抽屜佈局功能。我希望能夠將Drawer Layout作爲整個應用程序中可用的導航工具。要使用抽屜佈局,我用我的活動佈局的XML文件如下:如何在Android應用程序中圍繞PreferenceActivity包裝DrawerLayout?

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

    <!-- The main content view --> 
    <LinearLayout 
     android:id="@+id/main_content_layout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

    <!-- The navigation drawer --> 
    <ListView android:id="@+id/navigational_drawer" 
     android:layout_width="220dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:choiceMode="singleChoice" 
     android:divider="@android:color/transparent" 
     android:dividerHeight="0dp" 
     android:background="#FFFFFF"/> 
</android.support.v4.widget.DrawerLayout> 

不過,我有一個很大的包裹DrawerLayout圍繞PreferenceActivity困難。 PreferenceActivity是一種特殊的活動,我想保留它,因爲它可以幫助我在應用中保持Android偏好的外觀和風格;就是說,它使用addPreferencesFromResource()調用來加載首選項,並且不使用addContentView()調用來加載XML文件;首選項資源通常包含不喜歡包裝在DrawerLayouts中的PreferenceScreen標籤。

任何人都必須處理這個問題,你是如何解決它的?如果您在Android應用中擁有PreferenceActivity,並且您的DrawerLayout可用於同一活動,請分享您是如何做到的。任何幫助將非常感激。

編輯:如果我改變

<!-- The main content view --> 
<LinearLayout 
    android:id="@+id/main_content_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

<ListView 
    android:id="@android:id/list" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" /> 

那麼當PreferenceActivity被加載(因爲ID「列表」匹配什麼偏好活動期待的應用程序不會崩潰找);然而,抽屜沒有顯示。這幾乎就好像ListView「list」下面的所有內容都被忽略了一樣。

+0

我也想保留我現有的活動並動態添加抽屜。不是因爲PreferenceActivity,而是因爲有將近100個活動並將它們全部更改爲碎片需要一段時間。我能夠用Horizo​​ntalScrollView來做到這一點,但我覺得DrawerLayout會更乾淨。 –

回答

2

這一個花了我一段時間,但我能夠使我的應用程序的這項工作。我不知道它是否有所作爲,但我使用的是HoloEverywhere,所以我只假設這也適用於標準PreferenceActivity

在我PreferenceActivity當我推翻的setContentView()所有版本加載我自定義DrawerLayout這是像你這樣的,並具有與list標準Android ID的ListView。在過去的方法,我就確定調用onContentChanged()我叫super.setContentView()後:

@Override 
public void setContentView(int layoutResID) 
    { 
    setContentView(getLayoutInflater().inflate(layoutResID)); 
    } 

@Override 
public void setContentView(View view) 
    { 
    setContentView(view, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); 
    } 

@Override 
public void setContentView(View view, LayoutParams params) 
    { 

    // inflate the drawer layout 

    super.setContentView(R.layout.navigation_drawer_base_layout); 

    DrawerLayout dl = findViewById(R.id.drawer_layout); 

    // do stuff here initialize the DrawerLayout like add a DrawerToggle, etc 

    ...STUFF 

    // Call onContentsChanged() to let the Activity know it needs to refresh itself. 

    onContentChanged(); 

    } 

onBuildHeaders()我還是叫loadHeadersFromResource()爲我的不同PreferenceScreen S創建所有Header秒。

+0

謝謝喬恩。我很久以前就開始實施我自己的Activity來模仿PreferenceActivity的外觀和感覺。我會將您的回覆標記爲「已回答」。 – CocoaAficionado

+0

@jon我執行相同的代碼,因爲你寫在這裏,但它不工作,你能幫我plz,這是我的問題:http://stackoverflow.com/questions/37629855/how-to-set-navigation-drawer -in-preferenceactivity –