gpt4 book ai didi

android - 抽屉导航汉堡包图标丢失

转载 作者:搜寻专家 更新时间:2023-11-01 07:52:36 25 4
gpt4 key购买 nike

<分区>

我这里有一个用于设置抽屉导航的代码。一切正常,除了应该在那里点击并打开抽屉的汉堡包图标没有出现。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);
}

导航 Activity :

 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);
}

主 Activity .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" />

我不知道哪里出了问题。我在这里使用自定义工具栏也进行了交叉检查。但是没有任何效果。我不知道故障是否真的在这里。请帮助。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com