[英]How to add title in Navigation drawer layout?
[UPDATE] [UPDATE]
I solve the problem by adding addHeaderView : 我通过添加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. 我已经为每个项目创建了带有listview(图标+文本)的导航抽屉。
Thanks a lot, 非常感谢,
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. 您可以通过教您的
ListAdapter
返回标题行以及详细信息行,以与在任何其他ListView
添加标题相同的方式执行此操作。 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. 在低级别,这涉及覆盖
getViewTypeCount()
和getItemViewType()
ListAdapter
,并让getView()
知道行类型之间的差异。 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
. 或者,使用现有的高级实现,如https://github.com/emilsjolander/StickyListHeaders或http://code.google.com/p/android-amazing-listview/或搜索
android listview headers
时发现的任何其他实现android listview headers
。
Put a TextView
above a ListView
, and wrap it inside a vertical LinearLayout
. 将
TextView
放在ListView
上方,并将其包装在垂直LinearLayout
。 Give to your ListView android:layout_weight="1"
and android:layout_height="0dip"
给你的ListView
android:layout_weight="1"
和android:layout_height="0dip"
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.
在Activity的布局中,您可以添加例如LinearLayout,而不是在DrawerLayout中添加listView,您可以轻松添加分隔符和行。 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. 在Activity中,您可以将监听器添加到按钮中。
Hope that helps! 希望有所帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.