我們正在製作一款Android應用,並且還有一些我們想要添加的內容。 這是Gmail應用程序具有的效果。Android Studio導航抽屜,如Gmail應用
您可以選擇要查看哪個帳戶(並且應用的其餘部分將相應地執行)。
編輯:
我現在已經有一個(工作)導航欄,但我想要的東西是在頭一輪的圖標。我希望有人能夠選擇他們正在查看的用戶。
我們正在製作一款Android應用,並且還有一些我們想要添加的內容。 這是Gmail應用程序具有的效果。Android Studio導航抽屜,如Gmail應用
您可以選擇要查看哪個帳戶(並且應用的其餘部分將相應地執行)。
編輯:
我現在已經有一個(工作)導航欄,但我想要的東西是在頭一輪的圖標。我希望有人能夠選擇他們正在查看的用戶。
您想要的效果可以通過使用com.android.support:design
支持庫中的NavigationView
來實現。
你可以在here上找到完整的教程。您可以從該教程here下載完整的源代碼。
和here's another nice tutorial你可以按照。
但長話短說,該視圖分爲兩個主要部分,一個標題和一個菜單部分,以及您必須在XML上定義的每個部分。
自該教程:
頭查看
這種觀點基本上是導航 抽屜,其持有的個人資料圖片,姓名和電子郵件等,你需要 的頂部要在一個單獨的佈局文件中定義這個,我們只需要在 中查看一下。
菜單
這是你要顯示你的標題下的菜單中,我們在菜單文件夾定義 菜單,就像你定義菜單爲您溢出 菜單。所以基本上,NavigationView是一個容器,用於標題視圖 和菜單,您將在滑動抽屜中使用它。所以現在你可以通過 瞭解NavigationView我們可以開始製作我們的 導航抽屜。
考慮到這一點,像使用其他佈局一樣構建標題。菜單的定義有點像工具欄/動作條菜單。例如: -
navigation_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group
android:checkableBehavior="single">
<item
android:id="@+id/drawer_home"
android:checked="true"
android:icon="@drawable/icon_home"
android:title="@string/title_home"/>
<item
android:id="@+id/drawer_content"
android:icon="@drawable/icon_content"
android:title="@string/title_content"/>
<item
android:id="@+id/drawer_about"
android:icon="@drawable/icon_about"
android:title="@string/title_about"/>
<item
android:id="@+id/drawer_exit"
android:icon="@drawable/icon_exit"
android:title="@string/title_exit"/>
</group>
</menu>
然後,在你的Activity
你就得做出那樣的一個教程發現佈局,使用與DrawerLayout
一起NavigationView
。
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
>
<include
android:id="@+id/toolbar"
layout="@layout/tool_bar"/>
<FrameLayout
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_gravity="start"
app:headerLayout="@layout/header"
app:menu="@menu/navigation_menu"/>
</android.support.v4.widget.DrawerLayout>
您還可以創造一些Fragments
爲要使用此NavigationView
顯示每個屏幕。你這樣做之後,你的Activity
您可以通過實施NavigationView.OnNavigationItemSelectedListener
,這樣的處理選擇事件:
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
// Your Activity
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
Fragment fragment = null;
switch(menuItem.getItemId()) {
case R.id.drawer_home:
fragment = new YourFragment();
break;
case R.id.drawer_content:
fragment = new AnotherFragment();
break;
case R.id.drawer_about:
fragment = new AboutFragment();
break;
case R.id.drawer_exit:
// TODO - Prompt to exit.
finish();
break;
}
if (fragment == null) {
fragment = new YourFragment();
}
drawerLayout.closeDrawers();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, fragment)
.commit();
return true;
}
}
至於你的編輯,圖標可以通過ImageView
表示。並且要在多個配置文件之間導航,這取決於您如何在您的應用中實現該邏輯,但作爲「通用」答案,您可以使用類似Spinner
的內容來切換這些配置文件。
這些教程將幫助您完成這一步:
一旦你設置在您頭 ,處理t他選擇項目並相應地更改用戶配置文件。 (這最後一部分完全取決於您在應用中如何實現用戶配置文件)。但是,如果要開始,可以檢查android training site,更具體地說,是this part。
更完美的答案。前進#Mauker –
@IntelliJAmiya什麼?你能解釋一下你的意思嗎? – WarrenFaith
我看到一篇博客文章,從Android Studio 1.4中剖析了NavigationView模板:http://androidessence.com/creating-a-material-design-navigation-drawer/ – AdamMc331
您應該使用NavigationView
它提供了一個框架,便於通過菜單來實現材料導航 抽屜充氣導航項目的幫助下 資源。 Befor導航視圖中,我們很難通過定製適配器 使用listview或linearlayout製作材質 抽屜,但現在我們只需要在DrawerLayout中添加導航視圖, 其他所有內容都將由導航視圖處理。
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<!-- Your contents -->
<android.support.design.widget.NavigationView
android:id="@+id/navigation"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="@menu/my_navigation_items" />
</android.support.v4.widget.DrawerLayout>
對於這個要求,您可以檢查樣品
希望這有助於。
我覺得這個MaterialDrawer是你在找什麼。這個庫有很多例子。您可以直接使用該庫,也可以閱讀源代碼並實現自己的抽屜。
檢查最新的編輯,如果有幫助,不要忘記標記爲已接受。 ;) – Mauker
嘿,很抱歉打擾了,但你是怎麼在右邊加上「1」的? – Omer
@Omer這是Gmail應用程序的屏幕截圖。 – Tvde1