问题
[UPDATE]
I solve the problem by adding addHeaderView :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTitle = mDrawerTitle = getTitle();
mPlanetTitles = getResources().getStringArray(R.array.planets_array);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
LayoutInflater inflater = getLayoutInflater();
ViewGroup mTop = (ViewGroup)inflater.inflate(R.layout.header_listview_menu, mDrawerList, false);
mDrawerList.addHeaderView(mTop, null, false);
================================
My question is so simple !
I would like to how to add a title in a navigation drawer ?
I already created my navigation drawer with listview (icon+text) for each item.
Thanks a lot,

回答1:
Put a TextView
above a ListView
, and wrap it inside a vertical LinearLayout
. Give to your ListView android:layout_weight="1"
and android:layout_height="0dip"
回答2:
You would do that the same way as you would add headings in any other ListView
, by teaching your ListAdapter
to return heading rows as well as detail rows. At the low level, this involves overriding methods like getViewTypeCount()
and getItemViewType()
in your ListAdapter
, plus having getView()
know the difference between the row types. Or, use an existing high-level implementation like https://github.com/emilsjolander/StickyListHeaders or http://code.google.com/p/android-amazing-listview/ or any of the others found when searching for android listview headers
.
回答3:
Maybe it's a bit late but I think I have a simpler solution. In your Activity's layout, instead of adding a listView inside the DrawerLayout, you can add for example a LinearLayout, and you can easily add separators and rows. For example:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.astuetz.viewpager.extensions.PagerSlidingTabStrip
android:id="@+id/indicator"
android:layout_height="48dip"
android:layout_width="fill_parent"/>
<ViewPager
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:orientation="vertical"
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#111">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Separator 1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First button"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Separator 2"/>
</LinearLayout>
And in the Activity, you can add the listeners to the buttons.
Hope that helps!
来源:https://stackoverflow.com/questions/16942792/how-to-add-title-in-navigation-drawer-layout