如何在导航抽屉布局中添加标题?

时间:2021-09-16 06:28:16

[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,

非常感谢,

如何在导航抽屉布局中添加标题?

3 个解决方案

#1


5  

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"

将TextView放在ListView上方,并将其包装在垂直LinearLayout中。给你的ListView android:layout_weight =“1”和android:layout_height =“0dip”

#2


11  

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.

您可以通过教您的ListAdapter返回标题行以及详细信息行,以与在任何其他ListView中添加标题相同的方式执行此操作。在低级别,这涉及覆盖ListAdapter中的getViewTypeCount()和getItemViewType()等方法,并让getView()知道行类型之间的差异。或者,使用现有的高级实现,如https://github.com/emilsjolander/StickyListHeaders或http://code.google.com/p/android-amazing-listview/或搜索android时发现的任何其他实现listview标题。

#3


0  

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:

也许有点晚了,但我想我有一个更简单的解决方案。在Activity的布局中,您可以添加例如LinearLayout,而不是在DrawerLayout中添加listView,并且可以轻松添加分隔符和行。例如:

<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!

希望有所帮助!

#1


5  

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"

将TextView放在ListView上方,并将其包装在垂直LinearLayout中。给你的ListView android:layout_weight =“1”和android:layout_height =“0dip”

#2


11  

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.

您可以通过教您的ListAdapter返回标题行以及详细信息行,以与在任何其他ListView中添加标题相同的方式执行此操作。在低级别,这涉及覆盖ListAdapter中的getViewTypeCount()和getItemViewType()等方法,并让getView()知道行类型之间的差异。或者,使用现有的高级实现,如https://github.com/emilsjolander/StickyListHeaders或http://code.google.com/p/android-amazing-listview/或搜索android时发现的任何其他实现listview标题。

#3


0  

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:

也许有点晚了,但我想我有一个更简单的解决方案。在Activity的布局中,您可以添加例如LinearLayout,而不是在DrawerLayout中添加listView,并且可以轻松添加分隔符和行。例如:

<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!

希望有所帮助!