Android:导航栏添加按钮以打开导航抽屉

时间:2015-04-24 09:10:05

标签: java android button navigation-drawer navigationbar

我正在尝试在我的Android应用中导航栏左侧实现一个按钮。我想要这个按钮打开导航抽屉。现在我唯一能做的就是从左上角向右滑动打开它。

我试过谷歌,并阅读文档。有人可以帮忙吗? 我的代码MainActivity.java在下面添加。谢谢!

package safefence.tc.safefence;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.ActionBarDrawerToggle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;

import java.util.ArrayList;


public class MainActivity extends ActionBarActivity implements LocationListener {

    ArrayList<NavigationDrawerItem> listItems;
    DrawerLayout drawerLayout;
    ActionBarDrawerToggle drawerToggle;
    ListView list;


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





        listItems = new ArrayList<NavigationDrawerItem>();
            listItems.add(new NavigationDrawerItem("Home", R.drawable.ic_drawer));
            listItems.add(new NavigationDrawerItem("About", R.drawable.ic_drawer));
            listItems.add(new NavigationDrawerItem("Follow", R.drawable.ic_drawer));
            listItems.add(new NavigationDrawerItem("Log out", R.drawable.ic_drawer));


        drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
        drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.red_string,R.string.abc_action_mode_done) {
            public void onDrawerClosed(View view) {
                super.onDrawerClosed(view);
            }

            public void onDrawerOpened(View view) {
                super.onDrawerOpened(view);

            }
        };

        drawerLayout.setDrawerListener(drawerToggle);

        list = (ListView) findViewById(R.id.drawer);

        list.setAdapter(new NavDrawerAdapter(this, listItems));

        list.setOnItemClickListener(new DrawerItemClickListener());
    }


    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        boolean drawerOpen = drawerLayout.isDrawerOpen(list);
        return super.onPrepareOptionsMenu(menu);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) { }

    @Override
    public void onProviderEnabled(String provider) { }

    @Override
    public void onProviderDisabled(String provider) { }
}

XML文件

<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_height="match_parent"
    android:layout_width="match_parent" >

    <FrameLayout
        android:id="@+id/frame_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <fragment
            android:id="@+id/map"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:name="com.google.android.gms.maps.MapFragment">
            </fragment>


    </FrameLayout>

    <ListView
        android:id="@+id/drawer"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#111"
        android:layout_width="240dp"
        android:layout_height="match_parent">
        </ListView>
    </android.support.v4.widget.DrawerLayout>

1 个答案:

答案 0 :(得分:0)

添加图标

 drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.drawable.ic_drawer, R.string.red_string,R.string.abc_action_mode_done)

ic_drawer 将成为您的偶像。

如果您正在处理滑动菜单,请参阅this link.这是一个很好的教程。