我已经尝试了很多,但直到现在还没有达到任何结果, 我做了一个汉堡包菜单,我可以在操作栏上更改标题,但是对于第一个菜单,我需要显示一个我成功完成的图像,但是当我切换时,我想要用汉堡包菜单标题替换该图像对于汉堡的其他片段,当我应用图像时,标题不会显示在任何其他片段的动作栏上 下面是我的抽屉布局xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<!-- The navigation drawer -->
<ListView android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#ffffff"
android:dividerHeight="1px"
android:background="#5D5D5D"/>
用于在操作栏上显示图像的我的xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_horizontal">
<ImageView
android:id="@+id/icon"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:src="@drawable/home"
/>
<!--<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/search"
android:layout_gravity="center_horizontal"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"/>-->
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
我的NavigationDrawer活动
package com.statzhub.Home.Hamburger.ui;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBarDrawerToggle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.statzhub.Home.Hamburger.adapaters.NavigationDrawerAdapter;
import com.statzhub.Home.Hamburger.model.NavigationDrawerItem;
import com.statzhub.Home.HomeFragment;
import com.statzhub.R;
import java.util.ArrayList;
public class NavigationDrawerActivity extends ActionBarActivity {
private String[] drawerTitles ;
private TypedArray drawerIcons ;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ArrayList<NavigationDrawerItem> navigationItems;
private CharSequence mTitle;
private ActionBarDrawerToggle mDrawerToggle;
private FragmentManager fragManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation_drawer);
// get array of string and images to be displayed in drawer list
drawerTitles = getResources().getStringArray(R.array.navigation_drawer_items);
drawerIcons = getResources().obtainTypedArray(R.array.navigation_drawer_icons);
mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
mDrawerList = (ListView)findViewById(R.id.left_drawer);
//create array of getter setter class
navigationItems = new ArrayList<NavigationDrawerItem>();
// add items
for(int i=0;i<5;i++){
navigationItems.add(new NavigationDrawerItem(drawerTitles[i],drawerIcons.getResourceId(i, 0)));
}
// recycle typed array
drawerIcons.recycle();
// set adapter
mDrawerList.setAdapter(new NavigationDrawerAdapter(this, navigationItems));
// enabling action bar app icon and behaving it as toggle button
setupActionBar();
// getSupportActionBar().setIcon(android.R.color.transparent);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.string.menu_icon,// navigation menu toggle icon
R.string.app_name// nav open or close description for accessibility
){
public void onDrawerClosed(View view){
getSupportActionBar().setTitle(getTitle());
invalidateOptionsMenu();
}
public void onDrawerOpen(View view){
getSupportActionBar().setTitle(getTitle());
invalidateOptionsMenu();
}
};
// mDrawerLayout.setDrawerListener(mDrawerToggle);
fragManager = getSupportFragmentManager();
// set default page on navigation drawer
fragManager.beginTransaction().add(R.id.content_frame,new HomeFragment()).commit();
// set functionality on click of drawer items
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
displayView(position);
}
});
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// toggle nav drawer on selecting action bar app icon/title
if (mDrawerToggle.onOptionsItemSelected(item)) {
}return true;
}
private void displayView(int position) {
Fragment fragment = null;
// replace fragments by current one
switch(position){
case 0:
fragment = new HomeFragment();
break;
case 1:
fragment = new SearchFragment();
break;
case 2:
fragment = new FollowingFragment();
break;
case 3:
fragment = new FAQFragment();
break;
case 4:
fragment = new SettingsFragment();
break;
default:
break;
}
if(fragment != null) {
// put fragment in frame container
fragManager.beginTransaction().replace(R.id.content_frame,fragment).commit();
// update selected item and close drawer
mDrawerList.setItemChecked(position,true);
mDrawerList.setSelection(position);
getSupportActionBar().setDisplayShowTitleEnabled(true);
// setTitle(drawerTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
}
@Override
public void setTitle(CharSequence title) {
super.setTitle(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 toggle
mDrawerToggle.onConfigurationChanged(newConfig);
}
private void setupActionBar() {
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#5D5D5D")));
LayoutInflater inflater = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.action_bar, null);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
TextView titleTV = (TextView)findViewById(R.id.title);
getSupportActionBar().setCustomView(v);
// showing search on action bar
getSupportActionBar().setDisplayOptions(getSupportActionBar().getDisplayOptions()
| ActionBar.DISPLAY_SHOW_CUSTOM);
ImageView imageView = new ImageView(getSupportActionBar().getThemedContext());
imageView.setScaleType(ImageView.ScaleType.CENTER);
imageView.setImageResource(R.drawable.ic_action_search);
ActionBar.LayoutParams layoutParams = new ActionBar.LayoutParams(
ActionBar.LayoutParams.WRAP_CONTENT,
ActionBar.LayoutParams.WRAP_CONTENT, Gravity.RIGHT
| Gravity.CENTER_VERTICAL);
layoutParams.rightMargin = 20;
imageView.setLayoutParams(layoutParams);
getSupportActionBar().setCustomView(imageView);
}
}
我想在HomeFragment上显示Image,在其他片段中显示标题 我正在使用&#39; com.android.support:appcompat-v7:22.1.1&#39;动作栏库 我希望我的搜索图像应始终存在于所有片段中 很感谢任何形式的帮助。 感谢