2016-01-11 62 views
1

我想添加抽屜到我的應用程序,就像我在這張圖中畫的一樣。如何添加滑動左和圖像點擊抽屜Android

有沒有簡單的方法來做到這一點?

我不使用材質用戶界面。從空靈活動開始

有沒有辦法做到這一點?

支票影像

illustration of Drawer

+0

爲什麼我們不從Google搜索開始:「Android導航抽屜」 –

+0

http://www.viralandroid.com/2015/08/android-navigation-drawer-view.html勾選此項教程 – shanks

+0

@James_Parsons我在google上搜索了2,3天,然後在這裏發佈了大部分的教程,但它並沒有爲我工作,這就是爲什麼發佈這樣的問題 –

回答

2

您可以使用在V4支持庫的DrawerLayout。首先,添加以下依賴於你gradle這個文件:

compile 'com.android.support:support-v4:23.1.1' 

然後,在活動中,您希望有抽屜,與android.support.v4.widget.DrawerLayout替換根視圖。根應該包含子視圖,第一個將是主Activity,第二個子將是導航抽屜。現在

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

    <!-- Main Activity Content --> 
    <LinearLayout 
     android:id="@+id/main_content" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

    <!-- The navigation drawer --> 
    <ListView 
     android:id="@+id/drawer" 
     android:layout_width="240dp" 
     android:layout_height="match_parent" /> 

</android.support.v4.widget.DrawerLayout> 

,假設您瞭解如何設置ListView S,你應該能夠設置ListView並刷卡打開。

要添加的圖標打開了,你必須建立一個ActionBarDrawerToggle在您的活動,這樣做會是這個樣子(記得使用支持庫ActionBarDrawerToggle:在

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

ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, layout, "Open Drawer", "Close Drawer") { 
    public void onDrawerClosed(View view) { 
     super.onDrawerClosed(view); 
    } 

    public void onDrawerOpened(View drawerView) { 
     super.onDrawerOpened(drawerView); 
    } 
} 

// Set the toggle. 
layout.setDrawerListener(toggle); 

然後你onOptionsItemSelected

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    if (toggle.onOptionsItemSelected(item)) { 
     return true; 
    } 
} 

現在的圖標應該打開抽屜

Android的文檔有一個偉大的tutori al欲瞭解更多信息,請訪問:http://developer.android.com/training/implementing-navigation/nav-drawer.html

+0

非常感謝:D –