我正在尝试在我的app上实现导航抽屉,扩展ActionBarActivity并使用Theme.AppCompat.Light。屏幕与此帖drawer icon not left aligned and launcher icon not showing
中的屏幕类似应用程序图标未显示且ic_drawer图标未正确对齐。我看过有同样问题的帖子,但没有一个得到我可以使用的回复。
我已经在这个问题上花了一个星期没有结果。
提前谢谢你。
贝娄是代码:
public class NavigationDrawerActivity extends ActionBarActivity {
private String[] mNavigationDrawerItemTitles;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
//For app icon control for nav drawer
ActionBarDrawerToggle mDrawerToggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation_drawer);
mNavigationDrawerItemTitles= getResources().getStringArray(R.array.navigation_drawer_items_array);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
// set a custom shadow that overlays the main content when the drawer opens
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
ObjectDrawerItem[] drawerItem = new ObjectDrawerItem[3];
drawerItem[0] = new ObjectDrawerItem(R.drawable.ic_action_camera, "My Pets");
drawerItem[1] = new ObjectDrawerItem(R.drawable.ic_drawer, "Married");
drawerItem[2] = new ObjectDrawerItem(R.drawable.ic_action_help, "Help");
//Pass the list of drawer items to DrawerItemCustomAdapter
DrawerItemCustomAdapter adapter = new DrawerItemCustomAdapter(this, R.layout.drawer_listview_item_row, drawerItem);
//Set the adapter
mDrawerList.setAdapter(adapter);
//Set the item click listener
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
//Set properties for proper title display
mTitle = mDrawerTitle = getTitle();
//Add the app icon control code
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
R.drawable.ic_drawer,
R.string.drawer_open,
R.string.drawer_close
) {
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
getSupportActionBar().setTitle(mTitle);
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getSupportActionBar().setTitle(mDrawerTitle);
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
//getSupportActionBar().setIcon(R.drawable.ic_launcher);
//getSupportActionBar().setDisplayShowHomeEnabled(true);
}
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
}
private void selectItem(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = new MyPetListFragment();
break;
case 1:
fragment = new MyPetMarriedFragment();
break;
case 2:
fragment = new MyPetMarriedFragment();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(mNavigationDrawerItemTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
Log.e("MainActivity", "Error in creating fragment");
}
}
@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_navigation_drawer, menu);
//menu.findItem(R.id.action_settings).setVisible(true);
return true;
}
/* Called whenever we call invalidateOptionsMenu() */
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// If the nav drawer is open, hide action items related to the content view
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
menu.findItem(R.id.action_help).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
@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.
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action bar actions click
switch (item.getItemId()) {
case R.id.action_help:
Toast.makeText(getApplicationContext(), "Help Pressed",
Toast.LENGTH_LONG).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
//This is really needed for changing titles.
public void setTitle(CharSequence title) {
mTitle = title;
getSupportActionBar().setTitle(mTitle);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggles
mDrawerToggle.onConfigurationChanged(newConfig);
}
}`
和styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- the theme applied to the application or activity -->
<style name="AppTheme"
parent="@style/Theme.AppCompat.Light">
<item name="android:actionBarStyle">@style/MyActionBar</item>
<item name="android:actionBarTabTextStyle">@style/MyActionBarTabText</item>
<item name="android:actionMenuTextColor">@color/blue</item>
<!-- Support library compatibility -->
<item name="actionBarStyle">@style/MyActionBar</item>
<item name="actionBarTabTextStyle">@style/MyActionBarTabText</item>
<item name="actionMenuTextColor">@color/blue</item>
<item name="toolbarStyle">@style/myToolbarStyle</item>
<item name="toolbarNavigationButtonStyle">@style/myToolbarNavigationButtonStyle</item>
</style>
<!-- ActionBar styles -->
<style name="MyActionBar"
parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="android:titleTextStyle">@style/MyActionBarTitleText</item>
<item name="android:background">@color/darkbrown</item>
<!-- Support library compatibility -->
<item name="titleTextStyle">@style/MyActionBarTitleText</item>
<item name="background">@color/darkbrown</item>
</style>
<!-- ActionBar title text -->
<style name="MyActionBarTitleText"
parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
<item name="android:textColor">@color/darkblue</item>
<!-- The textColor property is backward compatible with the Support Library -->
</style>
<style name="myToolbarNavigationButtonStyle" parent="@style/Widget.AppCompat.Toolbar.Button.Navigation">
<item name="android:minWidth">0dp</item>
<item name="android:padding">12dp</item>
<item name="android:scaleType">centerInside</item>
</style>
<style name="myToolbarStyle" parent="@style/Widget.AppCompat.Toolbar">
<item name="android:paddingRight">0dp</item>
</style>
<!-- ActionBar tabs text -->
<style name="MyActionBarTabText"
parent="@style/Widget.AppCompat.ActionBar.TabText">
<item name="android:textColor">@color/darkred</item>
<!-- The textColor property is backward compatible with the Support Library -->
</style>
<style name="Text.Small" parent="@android:style/TextAppearance">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">@color/brownText</item>
<item name="android:typeface">monospace</item>
<item name="android:textSize">10sp</item>
</style>
<style name="Text.Small.Black" parent="@android:style/TextAppearance">
<item name="android:textColor">@color/black</item>
</style>
<style name="Text.Medium" parent="@android:style/TextAppearance">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">@color/brownText</item>
<item name="android:typeface">monospace</item>
<item name="android:textSize">20sp</item>
</style>
<style name="Text.Big" parent="@android:style/TextAppearance">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">@color/brownText</item>
<item name="android:typeface">monospace</item>
<item name="android:textSize">30sp</item>
</style>
</resources>