问题
I have a navigation draw with sections and items, but I need to put an image and a text as header but don't know how exactly from what I have, the navigation drawer that I have is the default one that android studio create when you right click and select new > activity > navigation drawer activity.
this is the xml that I want to have as header: nav_header.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Character's Avatar -->
<ImageView android:id="@+id/navigation_drawer_header_iv_image"
android:layout_width="200dp"
android:layout_height="200dp"
android:src="@drawable/image_sorcerer"/>
<!-- Character's name -->
<TextView android:id="@+id/navigation_drawer_header_tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Example text"
android:paddingLeft="16dp"/>
</LinearLayout>
The navigation drawer content is set through a custom adapter
GameNavigationFragment class (This is the fragment that manage the navigation drawer)
public class GameNavigationFragment extends Fragment {
private static final String STATE_SELECTED_POSITION = "selected_navigation_drawer_position";
private static final String PREF_USER_LEARNED_DRAWER = "navigation_drawer_learned";
private NavigationDrawerCallbacks mCallbacks;
private ActionBarDrawerToggle mDrawerToggle;
private DrawerLayout mDrawerLayout;
private ListView mDrawerListView;
private View mFragmentContainerView;
private int mCurrentSelectedPosition = 0;
private boolean mFromSavedInstanceState;
private boolean mUserLearnedDrawer;
public GameNavigationFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Read in the flag indicating whether or not the user has demonstrated awareness of the
// drawer. See PREF_USER_LEARNED_DRAWER for details.
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getActivity());
mUserLearnedDrawer = sp.getBoolean(PREF_USER_LEARNED_DRAWER, false);
if (savedInstanceState != null) {
mCurrentSelectedPosition = savedInstanceState.getInt(STATE_SELECTED_POSITION);
mFromSavedInstanceState = true;
}
// Select either the default item (0) or the last selected item.
selectItem(mCurrentSelectedPosition);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Indicate that this fragment would like to influence the set of actions in the action bar.
setHasOptionsMenu(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mDrawerListView = (ListView) inflater.inflate(
R.layout.fragment_navigation_drawer, container, false);
mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
});
CustomListViewAdapter adapter = new CustomListViewAdapter(getActivity());
adapter.addSeparatorItem("Menu");
adapter.addItem("Section 1");
adapter.addItem("Section 2");
adapter.addItem("Section 3");
adapter.addSeparatorItem("Opciones");
adapter.addItem("Section 1");
adapter.addItem("Section 2");
adapter.addItem("Section 3");
mDrawerListView.setAdapter(adapter);
return mDrawerListView;
}
CustomAdapterListView
package com.jomasa.pocketfantasy.adapters;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.TreeSet;
import com.jomasa.pocketfantasy.R;
public class CustomListViewAdapter extends BaseAdapter {
private static final int TYPE_ITEM = 0;
private static final int TYPE_SEPARATOR = 1;
private ArrayList<String> mData = new ArrayList<String>();
private TreeSet<Integer> sectionHeader = new TreeSet<Integer>();
private LayoutInflater mInflater;
public CustomListViewAdapter(Context context){
mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void addItem(final String item) {
mData.add(item);
notifyDataSetChanged();
}
public void addSeparatorItem(final String item) {
mData.add(item);
sectionHeader.add(mData.size() - 1);
notifyDataSetChanged();
}
@Override
public int getItemViewType(int position) {
return sectionHeader.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getCount() {
return mData.size();
}
@Override
public String getItem(int position) {
return mData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
int rowType = getItemViewType(position);
if (convertView == null) {
holder = new ViewHolder();
switch (rowType) {
case TYPE_ITEM:
convertView = mInflater.inflate(R.layout.snippet_item1, null);
holder.textView = (TextView) convertView.findViewById(R.id.text);
break;
case TYPE_SEPARATOR:
convertView = mInflater.inflate(R.layout.snippet_item2, null);
holder.textView = (TextView) convertView.findViewById(R.id.textSeparator);
break;
}
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.textView.setText(mData.get(position));
return convertView;
}
public static class ViewHolder {
public TextView textView;
}
}
GameActivity
package com.jomasa.pocketfantasy.activities;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.FragmentManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.widget.DrawerLayout;
import com.jomasa.pocketfantasy.R;
import com.jomasa.pocketfantasy.fragments.GameInventoryFragment;
import com.jomasa.pocketfantasy.fragments.GameMainFragment;
import com.jomasa.pocketfantasy.fragments.GameNavigationFragment;
import com.jomasa.pocketfantasy.fragments.GameSheetFragment;
public class GameActivity extends AppCompatActivity
implements GameNavigationFragment.NavigationDrawerCallbacks {
/**
* Fragment managing the behaviors, interactions and presentation of the navigation drawer.
*/
private GameNavigationFragment mGameNavigationFragment;
/**
* Used to store the last screen title. For use in {@link #restoreActionBar()}.
*/
private CharSequence mTitle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
mGameNavigationFragment = (GameNavigationFragment)
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mGameNavigationFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
}
@Override
public void onNavigationDrawerItemSelected(int position) {
FragmentManager fragmentManager = getSupportFragmentManager();
switch (position)
{
case 0:
fragmentManager.beginTransaction()
.replace(R.id.container, GameMainFragment.newInstance(position))
.commit();
break;
case 1:
fragmentManager.beginTransaction()
.replace(R.id.container, GameInventoryFragment.newInstance(position))
.commit();
break;
case 2:
fragmentManager.beginTransaction()
.replace(R.id.container, GameSheetFragment.newInstance(position))
.commit();
break;
default: break;
}
}
public void onSectionAttached(int number) {
switch (number) {
case 1:
mTitle = getString(R.string.navigation_title_main);
break;
case 2:
mTitle = getString(R.string.navigation_title_inventory);
break;
case 3:
mTitle = getString(R.string.navigation_title_sheet);
break;
}
}
public void restoreActionBar() {
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle(mTitle);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!mGameNavigationFragment.isDrawerOpen()) {
// Only show items in the action bar relevant to this screen
// if the drawer is not showing. Otherwise, let the drawer
// decide what to show in the action bar.
getMenuInflater().inflate(R.menu.menu_game, menu);
restoreActionBar();
return true;
}
return super.onCreateOptionsMenu(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.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
this is how navigation looks like right now:
回答1:
Update your android studio to 1.4 then you can create a navigation drawer activity with header image
回答2:
This is something I'm working into.
activity_main.xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawerLayout"
android:fitsSystemWindows="true">
<!-- Contenido Principal -->
<include layout="@layout/main_content"
android:id="@+id/mainContent"/>
<!-- Navigation Drawer -->
<android.support.design.widget.NavigationView
android:id="@+id/navigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/drawer_header"
app:menu="@menu/nav_menu"/>
</android.support.v4.widget.DrawerLayout>
I am currently using Navigation View pattern. See this property in the NavigationView:
app:headerLayout="@layout/drawer_header"
Where you need yo replace drawer_header with your layout file. If you want to use NavigationView, you have to add this dependency in your Gradle:
compile 'com.android.support:design:22.2.0'
Hope this helps.
来源:https://stackoverflow.com/questions/33125513/how-to-add-a-header-with-an-image-and-a-textview-to-my-navigation-drawer