我有一个带导航抽屉的工具栏,当应用程序运行导航时,工具栏内容向右移动,检查我的代码并告诉问题是什么?
//主要活动的布局
<?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"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include
android:id="@+id/toolbar"
layout="@layout/main_toolbar" />
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/toolbar" />
</RelativeLayout>
<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#efefef"
android:choiceMode="singleChoice"
android:divider="@android:color/black"
android:dividerHeight="0.1dp" />
</android.support.v4.widget.DrawerLayout>
//这里是工具栏的布局我添加了颜色,因此它清晰可见My Framelayout向右移动
<?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:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="170dp"
android:background="@drawable/dealsimage"
android:contentInsetEnd="0dp"
android:contentInsetLeft="0dp"
android:contentInsetRight="0dp"
android:contentInsetStart="0dp"
app:contentInsetEnd="0dp"
app:contentInsetLeft="0dp"
app:contentInsetRight="0dp"
app:contentInsetStart="0dp"
app:theme="@style/Base.Theme.AppCompat.Light.DarkActionBar">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:id="@+id/llUserPic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">
<de.hdodenhof.circleimageview.CircleImageView xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/ivProfileImage"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_gravity="center_horizontal"
android:src="@drawable/personicon"
android:visibility="visible"
app:civ_border_color="@android:color/transparent"
app:civ_border_width="2dp" />
<TextView
android:id="@+id/lbl_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Username"
android:textColor="@android:color/white"
android:textSize="24sp" />
<TextView
android:id="@+id/lbl_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Address"
android:textColor="@android:color/white"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/llTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/llUserPic"
android:layout_marginBottom="10dp"
android:layout_gravity="bottom"
android:orientation="vertical"
android:visibility="gone">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom">
<TextView
android:id="@+id/lbl_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"
android:textColor="@android:color/white"
android:textSize="25sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/llTimeFrame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom">
<TextView
android:id="@+id/storeDay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DAY"
android:textColor="@android:color/white"
android:textSize="18sp" />
<TextView
android:id="@+id/storeWeek"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:text="WEEK"
android:textColor="@android:color/white"
android:textSize="18sp" />
<TextView
android:id="@+id/storeMonth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:text="MONTH"
android:textColor="@android:color/white"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>
</FrameLayout>
</RelativeLayout>
</android.support.v7.widget.Toolbar>
// MainActivity Code
package com.cloudsolutions.senecahawk.activities;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.SearchManager;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.cloudsolutions.senecahawk.R;
import com.cloudsolutions.senecahawk.adapters.SideMenuAdapter;
import com.cloudsolutions.senecahawk.fragments.EmptyFragment;
import com.cloudsolutions.senecahawk.fragments.FragmentDealDetails;
import com.cloudsolutions.senecahawk.fragments.FragmentDeals;
import com.cloudsolutions.senecahawk.fragments.FragmentHome;
import com.cloudsolutions.senecahawk.models.NavItem;
import com.cloudsolutions.senecahawk.utilities.SessionManager;
import java.util.ArrayList;
/**
* Created by osama on 1/10/2016.
*/
public class MainActivity extends Activity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
public static String FRAGMENT_TAG = "";
Toolbar toolbar;
TextView toolbartitle;
TextView tvUsername;
ArrayList<NavItem> mNavItems = new ArrayList<NavItem>();
SessionManager session;
SharedPreferences pref;
int PRIVATE_MODE = 0;
private static final String PREF_NAME = "BuddyUrban";
public static final String KEY_NAME = "name";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.appmainactivity);
mTitle = mDrawerTitle = getTitle();
mNavItems.add(new NavItem("Home"));
mNavItems.add(new NavItem("Personal Information"));
mNavItems.add(new NavItem("Redemption History"));
mNavItems.add(new NavItem("Total Saving"));
mNavItems.add(new NavItem("Deal"));
mNavItems.add(new NavItem("Store"));
mNavItems.add(new NavItem("Refer a Friend"));
mNavItems.add(new NavItem("Logout"));
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbartitle = (TextView) findViewById(R.id.lbl_title);
tvUsername = (TextView) findViewById(R.id.lbl_username);
// set a custom shadow that overlays the main content when the drawer opens
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
// set up the drawer's list view with items and click listener
SideMenuAdapter adapter = new SideMenuAdapter(this, mNavItems);
mDrawerList.setAdapter(adapter);
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
if (savedInstanceState == null) {
selectItem(0);
}
// ActionBarDrawerToggle ties together the the proper interactions
// between the sliding drawer and the action bar app icon
mDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
toolbar, /* nav drawer image to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description for accessibility */
R.string.drawer_close /* "close drawer" description for accessibility */
) {
public void onDrawerClosed(View view) {
//getSupportActionBar().setTitle(mTitle);
toolbartitle.setText(mTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
//getSupportActionBar().setTitle(mDrawerTitle);
toolbartitle.setText(mDrawerTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
//getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//getSupportActionBar().setHomeButtonEnabled(true);
toolbartitle.setText("");
mDrawerToggle.syncState();
// toolbar.setContentInsetsAbsolute(0, 0);
session = new SessionManager(MainActivity.this);
pref = getSharedPreferences(PREF_NAME, PRIVATE_MODE);
String username = pref.getString(KEY_NAME, "");
tvUsername.setText(username.toUpperCase());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
/* 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_search).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// The action bar home/up action should open or close the drawer.
// ActionBarDrawerToggle will take care of this.
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action buttons
switch (item.getItemId()) {
case R.id.action_search:
// create intent to perform web search for this planet
Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
intent.putExtra(SearchManager.QUERY, toolbartitle.getText());
// catch event that there's no activity to handle intent
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
} else {
Toast.makeText(this, "Not Available", Toast.LENGTH_LONG).show();
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
/* The click listner for ListView in the navigation drawer */
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) {
// Load your conten here
FragmentManager fragmentManager = getFragmentManager();
Fragment fragment = EmptyFragment.newInstance();
if (position == 0) {
FRAGMENT_TAG = "FragmentHome";
fragment = FragmentHome.newInstance();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, fragment)
.commit();
} else if (position == 1) {
} else if (position == 2) {
} else if (position == 3) {
} else if (position == 4) {
} else if (position == 5) {
} else if (position == 6) {
} else if (position == 7) {
session.logoutUser();
}
mDrawerList.setItemChecked(position, true);
setTitle(mNavItems.get(position).getItem());
// Close the drawer
mDrawerLayout.closeDrawer(mDrawerList);
}
@Override
public void setTitle(CharSequence title) {
mTitle = title;
//getSupportActionBar().setTitle(mTitle);
toolbartitle.setText(mTitle);
}
/**
* When using the ActionBarDrawerToggle, you must call it during
* onPostCreate() and onConfigurationChanged()...
*/
@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 toggls
mDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
public void onBackPressed() {
if ("FragmentDeals".equals(FRAGMENT_TAG)) {
Fragment fragment = FragmentHome.newInstance();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, fragment)
.commit();
return;
} else if ("FragmentDealDetails".equals(FRAGMENT_TAG)) {
Fragment fragment = FragmentDeals.newInstance();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, fragment)
.commit();
return;
}
super.onBackPressed();
}
public static void setFragmentTag(String tag) {
FRAGMENT_TAG = tag;
}
public static String getFragmentTag() {
return FRAGMENT_TAG;
}
}