导航抽屉汉堡图标丢失

3
我有一段针对设置导航抽屉的代码。除了应该点击并打开抽屉的汉堡包图标未出现外,一切都正常工作。MainActivity:
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.defcomm.invento.NavigationDrawerActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

public class INVENTO extends AppCompatActivity {

private Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_invento);
    toolbar= (Toolbar) findViewById(R.id.app_bar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    NavigationDrawerActivity drawerFragment= (NavigationDrawerActivity)   getSupportFragmentManager().
            findFragmentById(R.id.navigation_drawer);
        drawerFragment.setUp(R.id.navigation_drawer, (DrawerLayout) findViewById(R.id.drawerlayout), toolbar);


}
@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_invento, 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);
}

}

Navigation avtivity:

 import android.content.Context;
 import android.content.SharedPreferences;
 import android.os.Bundle;
 import android.support.annotation.Nullable;
 import android.support.v4.app.Fragment;
  import android.support.v4.widget.DrawerLayout;
 import android.support.v7.app.ActionBarDrawerToggle;
 import android.support.v7.widget.Toolbar;
  import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;


 /**
 * A simple {@link Fragment} subclass.
*/
public class NavigationDrawerActivity extends Fragment {


private ActionBarDrawerToggle mdrawerToggle;
private DrawerLayout mdrawerLayout;

private boolean  mUserLearnedState;
View containerId;
public static final String file_pref_name="Testpef";
public static final String KEY_USER_VALUE="user_learned_drawer";
private boolean mfromSavedInstanceState;
public NavigationDrawerActivity() {
    // Required empty public constructor
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mUserLearnedState=Boolean.valueOf(readPreference(getActivity(),   KEY_USER_VALUE, "false"));
    if (savedInstanceState!=null){
        mfromSavedInstanceState=true;
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_navigation_drawer, container, false);

}


public void setUp(int fragmentId,DrawerLayout drawerlayout,Toolbar toolbar) {
    mdrawerLayout=drawerlayout;
    containerId=getActivity().findViewById(fragmentId);
    mdrawerToggle= new ActionBarDrawerToggle(getActivity(),drawerlayout,
            R.string.drawer_open,R.string.drawer_close){
        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            if(!mUserLearnedState){
                mUserLearnedState=true;
                saveToPreference(getActivity(),KEY_USER_VALUE,mUserLearnedState+"");
            }
            getActivity().invalidateOptionsMenu();
        }

        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
            mdrawerToggle.setDrawerIndicatorEnabled(true);
            getActivity().invalidateOptionsMenu();
        }
    };
    if(!mUserLearnedState&&!mfromSavedInstanceState){
        mdrawerLayout.openDrawer(containerId);
    }

    mdrawerLayout.setDrawerListener(mdrawerToggle);

    mdrawerLayout.post(new Runnable() {
        @Override
        public void run() {
            mdrawerToggle.syncState();
            mdrawerToggle.setDrawerIndicatorEnabled(true);
        }
    });

}
public static void saveToPreference (Context context,String preferenceName,String preferenceValue){
    SharedPreferences shared= context.getSharedPreferences(file_pref_name, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor=shared.edit();
    editor.putString(preferenceName,preferenceValue);
    editor.apply();
}
public static String readPreference(Context context,String preferenceName,String defaultValue){
    SharedPreferences share= context.getSharedPreferences(file_pref_name,Context.MODE_PRIVATE);
    return share.getString(preferenceName,defaultValue);
}

}

mainactivity.xml

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

<RelativeLayout  android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <include
        layout="@layout/app_bar"
        android:id="@+id/app_bar"/>
    <TextView
        android:layout_below="@+id/app_bar"
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>
<fragment
    android:id="@+id/navigation_drawer"
    android:layout_width="@dimen/drawer_width"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:layout="@layout/fragment_navigation_drawer"
    android:name="com.defcomm.invento.NavigationDrawerActivity"
    tools:layout="@layout/fragment_navigation_drawer" />

我不知道这里出了什么问题。我在这里使用自定义工具栏并进行了交叉检查,但是仍然无法正常工作。我不确定是否真的是这里出了故障。请帮忙解决。


我的mainactivity.xml文件长这样: - Midhun
3个回答

3
  final ActionBar actionBar = getSupportActionBar();
    if(actionBar!=null)
    {
        actionBar.setHomeAsUpIndicator(android.R.drawable.ic_drawer);
        actionBar.setDisplayHomeAsUpEnabled(true);
    }

在任何活动中使用上述代码来放置导航抽屉的图标。您甚至可以在不同的活动中更改它。

使用以下代码 --

 @Override
    public void onBackPressed(){
    if(drawer.isDrawerOpen()){ //replace this with actual function which returns        
     drawer.close();    
     // replace this with actual function which closes      drawer
       }
      else{
       super.onBackPressed();
      }
    }

先生,我在我的项目中使用了这种方法,但是在点击它时抽屉没有弹出。请帮帮我。虽然按钮正在显示。 - Midhun
1
让我给你一个简单的解决方案,这将帮助你理解。并且会给你一个预先编写好的代码。 如果你正在使用Eclipse,那么点击File>New>Others>Android>AndroidActivityNavigationDrawer 在Android Studio中也是类似的。右键单击包>New File>Activity>NavigationDrawer activity。尝试一下,你就会理解所有的流程。 - Ankit Gupta
请提供需要翻译的具体内容。 - Ankit Gupta
我尝试打这个,但是出现了错误。也许我不知道在哪里输入,请告诉我应该在哪里输入。 - Midhun

1
使用此主题在 manifest.xml 中配置你的活动:manifest.xml
<style name="AppTheme" parent="Theme.AppCompat.Light">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/primaryColor</item>
        <item name="colorPrimaryDark">@color/primaryColor</item>
        <item name="colorAccent">@color/primaryColor</item>
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:textColorPrimary">@android:color/white</item>
        <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
        <item name="actionMenuTextColor">@android:color/holo_blue_dark</item>
        <item name="android:actionMenuTextColor">@android:color/holo_blue_dark</item>
        <item name="android:windowBackground">@android:color/white</item>
        <item name="android:screenOrientation">portrait</item>
    </style>

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

    <style name="ToolBarStyle" parent="">
        <item name="popupTheme">@style/ThemeOverlay.AppCompat.Light</item>
        <item name="theme">@style/ThemeOverlay.AppCompat.Light</item>
    </style>

而对于API 21及以上,请使用此代码

<style name="AppTheme" parent="Theme.AppCompat.Light">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/primaryColor</item>
        <item name="colorPrimaryDark">@color/primaryColor</item>
        <item name="colorAccent">@color/primaryColor</item>
        <item name="android:textColorPrimary">@android:color/white</item>
        <item name="android:navigationBarColor">@android:color/black</item>
        <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
        <item name="actionMenuTextColor">@android:color/holo_blue_dark</item>
        <item name="android:actionMenuTextColor">@android:color/holo_blue_dark</item>
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:statusBarColor">@android:color/black</item>
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowBackground">@android:color/white</item>
        <item name="android:windowContentTransitions">true</item>
        <item name="android:screenOrientation">portrait</item>
    </style>

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

    <style name="ToolBarStyle" parent="">
        <item name="android:elevation">@dimen/toolbar_elevation</item>
        <item name="android:transitionName">actionBar</item>
        <item name="popupTheme">@style/ThemeOverlay.AppCompat.Light</item>
        <item name="theme">@style/ThemeOverlay.AppCompat.Light</item>
    </style>

接下来在您的MainActivity中设置工具栏,如下所示

mToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setTitle("Your Title");

在你的activity的onCreate中调用这个方法。
setupNavigationView();

这里是上面的方法

private void setupNavigationView() {
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);

        ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar, R.string.drawer_open, R.string.drawer_close) {

            @Override
            public void onDrawerClosed(View drawerView) {
                // Code here will be triggered once the drawer closes as we dont want anything to happen so we leave this blank
                super.onDrawerClosed(drawerView);
            }

            @Override
            public void onDrawerOpened(View drawerView) {
                // Code here will be triggered once the drawer open as we dont want anything to happen so we leave this blank

                super.onDrawerOpened(drawerView);
            }
        };

        //Setting the actionbarToggle to drawer layout
        mDrawerLayout.setDrawerListener(actionBarDrawerToggle);

        //calling sync state is necessay or else your hamburger icon wont show up
        actionBarDrawerToggle.syncState();


    }

检查这个编辑过的答案,它已经测试过并且完美运行! - Shishram

0

好的,对于这一切我很抱歉。我的一个错误是,我没有将自定义工具栏作为参数传递给 mdrawerToggle。现在它已经正常工作了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接