15

我在Android 5.0預覽版本中使用Nexus 7。棒棒糖遊戲商店中的抽屜指示器

在此頁面 http://developer.android.com/tools/support-library/index.html

我看到

變更爲V7程序兼容性庫:更新ActionBarDrawerToggle,其中 包含菜單對箭頭動畫

這是什麼Google Play應用使用?我是否讀了太多這個陳述?我需要更改以獲取此行爲 - 我無法在API文檔中找到它。

+0

是的,你只需要使用ActionBarDrawerToggle從程序兼容性-V7(不老的一個,從支持-V4)與內定指標。 – alanv 2014-10-17 18:13:12

回答

12

這很容易。

您的使用DrawerLayout的佈局看起來一如既往。您可以使用android.support.v4.widget.DrawerLayout和創建抽屜和內容區域:

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

<RelativeLayout 
    android:id="@+id/content_frame" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#ff0" 
    android:orientation="vertical" > 
</RelativeLayout> 

<ListView 
    android:id="@+id/leftDrawer" 
    android:layout_width="290dp" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    android:background="#f0f" 
    android:choiceMode="singleChoice" 
    android:clickable="true" 
    android:divider="@null" 
    android:dividerHeight="0dp" 
    android:scrollbars="none" /> 

主要的變化是在Java代碼中。在您的活動中,使用抽屜佈局的地方,您必須將其從V7擴展到ActionBarActivity。然後你爲DrawerLayout和ActionBarDrawerToggle創建變量。你的進口應該是這樣的:

import android.support.v4.widget.DrawerLayout; 
import android.support.v7.app.ActionBarDrawerToggle; 
import android.support.v7.app.ActionBarActivity; 

然後只是連接一切。請記住,新的抽屜佈局沒有圖標!你只是不會將它傳遞到你通常應該在的地方。代碼爲我的活動:

import android.content.res.Configuration; 
import android.os.Bundle; 
import android.support.v4.widget.DrawerLayout; 
import android.support.v7.app.ActionBarDrawerToggle; 
import android.support.v7.app.ActionBarActivity; 
import android.view.MenuItem; 

public class MainActivity extends ActionBarActivity { 

    DrawerLayout drawerLayout; 
    ActionBarDrawerToggle drawerToggle; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout); 
     drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.app_name, R.string.app_name) {}; 

     drawerLayout.setDrawerListener(drawerToggle); 

     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
     getSupportActionBar().setHomeButtonEnabled(true); 
    } 

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

    @Override 
    protected void onPostCreate(Bundle savedInstanceState) { 
     super.onPostCreate(savedInstanceState); 
     drawerToggle.syncState(); 
    } 

    @Override 
    public void onConfigurationChanged(Configuration newConfig) { 
     super.onConfigurationChanged(newConfig); 
     drawerToggle.onConfigurationChanged(newConfig); 
    } 

} 

它應該工作。

+0

是否需要使用兼容性ActionBarActivity? – 2014-10-17 22:01:25

+0

如果您使用支持庫和'NoActionBar'主題中的'Toolbar'小部件,則應該使用'ActionBarActivity'來使用'setActionBar(Toolbar)'方法。在這兩種情況下,如果你想支持舊版本,你應該使用。 – 2014-10-18 01:09:51

+0

謝謝分享的人:) – user3307005 2015-08-25 00:54:01

23

我已經張貼在這裏一個示例應用程序使用新的工具欄類,並ActionBarToggle提供Play商店風格動畫圖標的動作條:

https://github.com/03lafaye/LollipopDrawerToggle

無V7支持主幹採用ActionBarToggle框架活動和工具欄。主分支使用v7工具欄和一個ActionBarActivity。

不使用ActionBarActivity的設置是這樣的:

package com.plafayette.lollipop; 

import android.app.Activity; 
import android.support.v4.widget.DrawerLayout; 
import android.os.Bundle; 
import android.support.v7.app.ActionBarDrawerToggle; 
import android.widget.Toolbar; 
import android.view.Menu; 
import android.view.MenuItem; 

public class ToolbarActivity extends Activity { 
    private ActionBarDrawerToggle toggle; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_toolbar); 

     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setActionBar(toolbar); 

     DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
     toggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.open, R.string.close); 
     toggle.setDrawerIndicatorEnabled(true); 
     drawerLayout.setDrawerListener(toggle); 
    } 

    @Override 
    protected void onPostCreate(Bundle savedInstanceState) { 
     super.onPostCreate(savedInstanceState); 
     toggle.syncState(); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.toolbar, menu); 
     return true; 
    } 

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

     int id = item.getItemId(); 
     return id == R.id.action_settings || super.onOptionsItemSelected(item); 
    } 
} 

請注意,您必須禁用窗口動作條和標題欄在你的主題,像這樣:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <style name="AppTheme" parent="android:Theme.Material.Light"> 
     <item name="android:windowActionBar">false</item> 
     <item name="android:windowNoTitle">true</item> 
    </style> 
</resources> 

我想象最新的v7 appcompat庫的示例代碼將很快發佈,使得這篇文章過時。

Square的Chris Renke發佈了向上圖標動畫的備用回溯。代碼在GitHub上:https://github.com/ChrisRenke/DrawerArrowDrawable,他在http://chrisrenke.com/drawerarrowdrawable上寫了一篇關於它的博客。

+0

非常感謝,真的很有幫助! – Jaydeep 2014-10-23 01:49:21

+0

當抽屜項目被選中時,如何讓抽屜圖標不會變成後退箭頭?我希望圖標在所有抽屜碎片中保持不變。 – Sndn 2015-02-11 11:48:46

9

你看起來有一個工作抽屜,如果沒有,Documentation on "Creating a Navigation Drawer"是相當不錯的。

更新ActionBarDrawerToggle,其中包含了菜單到箭頭動畫

enter image description here

以上報價是指Create a new ActionBarDrawerToggle with arrow and hamburger menu提交。 作爲一個相關的:setDrawerIndicatorEnabled被添加到Add ability to disable drawer indicator in new ArrowDrawer

所以確保setDrawerIndicatorEnabled不調用false和使用

import android.support.v7.app.ActionBarDrawerToggle; 

,而不是

import android.support.v4.app.ActionBarDrawerToggle; 

這應該是很明顯的,從廢棄警告反正:

請@deprecated在support-v7-appcompat中使用ActionBarDrawerToggle。

恐怕還需要

// <item name="displayOptions">showHome|homeAsUp</item> 
getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
getSupportActionBar().setDisplayShowHomeEnabled(true); 
8

首先,確保你更新到最新的SDK。在Android Studio中創建新項目,然後在buid.gradle中添加appcompat-v7.21.0。+和appcompat-v4.21.0。+庫作爲gradle依賴項。

compile 'com.android.support:appcompat-v7:21.0.2' 
compile 'com.android.support:support-v4:21.0.2' 

在color.xml文件中添加primaryColor和primarycolorDark。

<resources> 
<color name="primaryColor">#2196F3</color> 
<color name="primaryColorDark">#0D47A1</color> 
</resources> 

在您的strings.xml文件中添加抽屜打開/關閉字符串值。

<resources> 
<string name="app_name">Lollipop Drawer</string> 
<string name="action_settings">Settings</string> 
<string name="drawer_open">open</string> 
<string name="drawer_close">close</string> 
</resources> 

你activity_my.xml佈局文件看起來是這樣的:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:orientation="vertical" 
android:layout_height="match_parent" 
tools:context=".MainActivity"> 

<include layout="@layout/toolbar" /> 


<android.support.v4.widget.DrawerLayout 
    android:layout_width="match_parent" 
    android:id="@+id/drawerLayout" 
    android:layout_height="match_parent"> 

    <!-- activity view --> 
    <RelativeLayout 
     android:layout_width="match_parent" 
     android:background="#fff" 
     android:layout_height="match_parent"> 

     <TextView 
      android:layout_centerInParent="true" 
      android:layout_width="wrap_content" 
      android:textColor="#000" 
      android:text="Activity Content" 
      android:layout_height="wrap_content" /> 
    </RelativeLayout> 

    <!-- navigation drawer --> 
    <RelativeLayout 
     android:layout_gravity="left|start" 
     android:layout_width="match_parent" 
     android:background="#fff" 
     android:layout_height="match_parent"> 

     <ListView 
      android:id="@+id/left_drawer" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:divider="#eee" 
      android:background="#fff" 
      android:dividerHeight="1dp" /> 
    </RelativeLayout> 

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

</LinearLayout> 

你toolbar.xml佈局文件看起來是這樣的:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/toolbar" 
android:minHeight="?attr/actionBarSize" 
android:background="?attr/colorPrimary" 
android:layout_width="match_parent" 
android:layout_height="wrap_content"> 

</android.support.v7.widget.Toolbar> 

你MyActivity.java看起來是這樣的: 在這裏你的活動必須擴展ActionBarActivity並將你的工具欄設置爲支持操作欄。

import android.content.res.Configuration; 
import android.support.v4.widget.DrawerLayout; 
import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.support.v7.app.ActionBarDrawerToggle; 
import android.support.v7.widget.Toolbar; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 

public class MyActivity extends ActionBarActivity { 

private Toolbar toolbar; 
private DrawerLayout drawerLayout; 
private ActionBarDrawerToggle drawerToggle; 
private ListView leftDrawerList; 
private ArrayAdapter<String> navigationDrawerAdapter; 
private String[] leftSliderData = {"Home", "Android", "Sitemap", "About", "Contact Me"}; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_my); 
    nitView(); 
    if (toolbar != null) { 
     toolbar.setTitle("Navigation Drawer"); 
     setSupportActionBar(toolbar); 
    } 
    initDrawer(); 
} 

private void nitView() { 
    leftDrawerList = (ListView) findViewById(R.id.left_drawer); 
    toolbar = (Toolbar) findViewById(R.id.toolbar); 
    drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout); 
    navigationDrawerAdapter=new ArrayAdapter<String>(MyActivity.this, android.R.layout.simple_list_item_1, leftSliderData); 
    leftDrawerList.setAdapter(navigationDrawerAdapter); 
} 

private void initDrawer() { 

    drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close) { 

     @Override 
     public void onDrawerClosed(View drawerView) { 
      super.onDrawerClosed(drawerView); 

     } 

     @Override 
     public void onDrawerOpened(View drawerView) { 
      super.onDrawerOpened(drawerView); 

     } 
    }; 
    drawerLayout.setDrawerListener(drawerToggle); 
} 

@Override 
protected void onPostCreate(Bundle savedInstanceState) { 
    super.onPostCreate(savedInstanceState); 
    drawerToggle.syncState(); 
} 

@Override 
public void onConfigurationChanged(Configuration newConfig) { 
    super.onConfigurationChanged(newConfig); 
    drawerToggle.onConfigurationChanged(newConfig); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.my, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    int id = item.getItemId(); 
    if (id == R.id.action_settings) { 
     return true; 
    } 
    if (drawerToggle.onOptionsItemSelected(item)) { 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 
} 

爲Android棒棒糖創建值-21文件夾style.xml文件

<?xml version="1.0" encoding="utf-8"?> 
<resources> 

<style name="myAppTheme" parent="Theme.AppCompat.Light.NoActionBar"> 
    <item name="colorPrimary">@color/primaryColor</item> 
    <item name="colorPrimaryDark">@color/primaryColorDark</item> 
    <item name="android:statusBarColor">@color/primaryColorDark</item> 

    <item name="drawerArrowStyle">@style/DrawerArrowStyle</item> 
</style> 

<style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle"> 
    <item name="spinBars">true</item> 
    <item name="color">@android:color/black</item> 
</style> 

</resources> 

創建值文件夾中的文件style.xml舊版本的Android,然後棒棒糖

<resources> 

<style name="myAppTheme" parent="Theme.AppCompat.Light"> 
    <item name="colorPrimary">@color/primaryColor</item> 
    <item name="colorPrimaryDark">@color/primaryColorDark</item> 
    <item name="android:windowNoTitle">true</item> 
    <item name="windowActionBar">false</item> 
    <item name="drawerArrowStyle">@style/DrawerArrowStyle</item> 
</style> 

<style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle"> 
    <item name="spinBars">true</item> 
    <item name="color">@android:color/black</item> 
</style> 

</resources> 

你AndroidManifest。XML是看起來像這樣:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="nkdroid.com.lollipopdrawer" > 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/myAppTheme" > 
    <activity 
     android:name=".MyActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application> 

</manifest> 

僅供參考: 你可以從這裏下載完整的源代碼click here

+0

非常感謝you.working罰款:) – Steve 2015-01-27 04:30:42

10

退房here

enter image description here

MainActivity.java :

package com.poliveira.apps.materialtests; 

import android.os.Bundle; 
import android.support.v4.widget.DrawerLayout; 
import android.support.v7.app.ActionBarActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.Menu; 
import android.widget.Toast; 


public class MainActivity extends ActionBarActivity implements NavigationDrawerCallbacks { 

    private Toolbar mToolbar; 
    private NavigationDrawerFragment mNavigationDrawerFragment; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     mToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar); 
     setSupportActionBar(mToolbar); 
     getSupportActionBar().setDisplayShowHomeEnabled(true); 

     mNavigationDrawerFragment = (NavigationDrawerFragment) getFragmentManager().findFragmentById(R.id.fragment_drawer); 
     mNavigationDrawerFragment.setup(R.id.fragment_drawer, (DrawerLayout) findViewById(R.id.drawer), mToolbar); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.main, menu); 
     return super.onCreateOptionsMenu(menu); 
    } 

    @Override 
    public void onNavigationDrawerItemSelected(int position) { 
     Toast.makeText(this, "Menu item selected -> " + position, Toast.LENGTH_SHORT).show(); 
    } 

    @Override 
    public void onBackPressed() { 
     if (mNavigationDrawerFragment.isDrawerOpen()) 
      mNavigationDrawerFragment.closeDrawer(); 
     else 
      super.onBackPressed(); 
    } 
} 

activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <include 
     android:id="@+id/toolbar_actionbar" 
     layout="@layout/toolbar_default" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"/> 

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

     <FrameLayout 
      android:id="@+id/container" 
      android:layout_width="match_parent" 
      android:clickable="true" 
      android:layout_height="match_parent"/> 

     <!-- android:layout_marginTop="?android:attr/actionBarSize"--> 
     <fragment 
      android:id="@+id/fragment_drawer" 
      android:name="com.poliveira.apps.materialtests.NavigationDrawerFragment" 
      android:layout_width="@dimen/navigation_drawer_width" 
      android:layout_height="match_parent" 
      android:layout_gravity="start" 
      app:layout="@layout/fragment_navigation_drawer"/> 
    </android.support.v4.widget.DrawerLayout> 
</RelativeLayout> 
+2

這在我看來是問題的最佳答案,直接點與乾淨的項目鏈接 – Maya 2015-01-19 18:20:13

+0

如果你需要移動你需要使用相同的佈局和相同的類實現從這個活動到另一個?例如,如果我想要在新活動中顯示操作欄,我是否需要將新活動擴展爲基於操作欄? – Maya 2015-01-20 20:20:56

+0

@Maya我沒有得到你。你必須以正確的方式實現預期的結果 – Steve 2015-01-21 04:40:50