主页按钮没有出现在导航抽屉android

时间:2015-06-14 16:53:22

标签: android navigation navigation-drawer

我正在使用navigation drawer进行项目,一切正常,但是一个小问题,我始终arrow显示的不是home按钮。 我尝试了在文档,论坛中找到的所有内容,这里没有任何实际工作。

到目前为止我尝试了什么:

mDrawerToggle.syncState(); 
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);




@Override
    public void onPostCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
        super.onPostCreate(savedInstanceState, persistentState);
        mNavigationDrawerFragment.getmDrawerToggle().syncState();
    }

ActionBar actionBar = getSupportActionBar();


actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
actionBar.setTitle(mTitle);

NavigationDrawerFragment:

// Defer code dependent on restoration of previous instance state.
        mDrawerLayout.post(new Runnable() {
            @Override
            public void run() {
                mDrawerToggle.syncState();
            }
        });

2 个答案:

答案 0 :(得分:0)

我一直在忙着用导航抽屉制作一个简单的应用程序..不要使用模板有不推荐的方法.. 这是完全有效..我创建了这个例子..试试这个..

您的MainActivity

public class Profile extends Fragment {

ActionBarDrawerToggle drawerToggle;
DrawerLayout mDrawerLayout;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.profile, container, false);
    return view;
}

public void setUp(DrawerLayout drawerLayout, Toolbar toolbar) {

    mDrawerLayout = drawerLayout;
    drawerToggle = new ActionBarDrawerToggle(getActivity(), drawerLayout, toolbar, R.string.open, R.string.close){

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

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

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {

            if (item != null && item.getItemId() == android.R.id.home) {
                if (mDrawerLayout.isDrawerOpen(Gravity.RIGHT)) {
                    mDrawerLayout.closeDrawer(Gravity.RIGHT);
                } else {
                    mDrawerLayout.openDrawer(Gravity.RIGHT);
                }
                return true;
            }
            return false;
        }

    };
    mDrawerLayout.setDrawerListener(drawerToggle);


    mDrawerLayout.post(new Runnable(){

        @Override
        public void run(){

            drawerToggle.syncState();
        }

    });
}
}

这是你的碎片..

 <?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:id="@+id/drawer"
android:layout_height="match_parent" >


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <include
        android:id="@+id/app_toolbar"
        layout="@layout/toolbar"/>
</LinearLayout>

<fragment
    android:id="@+id/profileFragmentInMain"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:layout="@layout/profile"
    android:name="com.example.ajay.myapplication.Profile"
    tools:layout="@layout/profile"/>

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

这都属于班级..

现在是xml layouts ..

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:theme="@android:style/Theme.Holo.Light.DialogWhenLarge.NoActionBar">

</LinearLayout>

your profile.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
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="wrap_content"
android:id="@+id/app_toolbar" />

你的toolbar.xml

where ... in

预览:

打开抽屉 enter image description here

关闭的抽屉 enter image description here

附加信息:我添加了工具栏,它是ActionBar的Material Design实现。它创建Actionbar的最新方式。你可以用它。为了以防万一,请在styles.xml文件中选择带 NoActionbBar 的主题。

答案 1 :(得分:0)

So I finally found the solution. I just took of this R.drawable.ic_drawer from:

 drawerToggle = new ActionBarDrawerToggle(getActivity(), drawerLayout, R.drawable.ic_drawer, R.string.open, R.string.close);

And it worked like a charm.