我是Android的新手。我在应用程序中添加了菜单图标。这是我的代码。
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item
android:id="@+id/action_settings"
android:title="@string/action_settings"
android:orderInCategory="1"
android:showAsAction="never" />
<item
android:id="@+id/action_example"
android:title="Logout"
android:orderInCategory="2"
android:showAsAction="withText|ifRoom" />
<item
android:id="@+id/addLoc"
android:title=""
android:orderInCategory="1"
app:showAsAction="ifRoom"
android:icon="@drawable/ic_location"
android:onClick="gotoLocation"/>
<item
android:id="@+id/dealsPic"
android:title=""
android:orderInCategory="2"
app:showAsAction="ifRoom"
android:icon="@drawable/ic_deals"
android:onClick="doThis"/>
<item
android:id="@+id/profPicture"
android:title=""
android:orderInCategory="3"
app:showAsAction="ifRoom"
android:icon="@drawable/ic_prof"
android:onClick="userProfile"
/>
<item
android:id="@+id/namePerson"
android:title="Nathalia Smith"
android:orderInCategory="4"
app:showAsAction="ifRoom"
android:onClick="userProfile"/>
</menu>
将这些菜单图标添加到我的操作栏后,出现了菜单图标。但是,当我展开导航抽屉时,图标会消失,而当我关闭它时,图标会出现。
我需要的是在打开抽屉时出现这些图标。
这是我的onCreate。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNavigationDrawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle(); //
// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#FFA500"))); // change the color of header
mTitle = mDrawerTitle = getTitle();
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
// enabling action bar app icon and behaving it as toggle button
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_menu);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,R.drawable.ic_menu, R.string.app_name,R.string.app_name)
{
public void onDrawerClosed(View view) {
getSupportActionBar().setTitle(mTitle);
// calling onPrepareOptionsMenu() to show action bar icons
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getSupportActionBar().setTitle(mDrawerTitle);
// calling onPrepareOptionsMenu() to hide action bar icons
invalidateOptionsMenu(); }
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
getSupportActionBar().setIcon(R.drawable.ic_deals);
}
这是我的onDrawerClosed和onDrawerOpened
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,R.drawable.ic_menu, R.string.app_name,R.string.app_name)
{
public void onDrawerClosed(View view) {
getSupportActionBar().setTitle(mTitle);
// calling onPrepareOptionsMenu() to show action bar icons
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getSupportActionBar().setTitle(mDrawerTitle);
// calling onPrepareOptionsMenu() to hide action bar icons
invalidateOptionsMenu(); }
};
我需要的是在打开抽屉时出现这些图标。有人请帮忙。
提前致谢。:)
invalidateOptionsMenu();
从下面的代码中删除为:
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,R.drawable.ic_menu, R.string.app_name,R.string.app_name)
{
public void onDrawerClosed(View view) {
getSupportActionBar().setTitle(mTitle);
}
public void onDrawerOpened(View drawerView) {
getSupportActionBar().setTitle(mDrawerTitle);
}
};
另外,如果您希望始终显示图标,则使用app:showAsAction="always"
如下所示:
<item
android:id="@+id/dealsPic"
android:title=""
android:orderInCategory="2"
app:showAsAction="always"
android:icon="@drawable/ic_deals"
android:onClick="doThis"/>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句