抽屉导航(添加列表视图和图像的标题)

时间:2014-03-26 04:24:09

标签: android

我正在使用Android开发网站中的抽屉导航,但它没有在列表视图中显示标题或标题或图像,所以你能告诉我如何在android开发中的抽屉导航中添加标题和图像网站没有? Like this example

以下是一些代码:<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"> <!-- As the main content view, the view below consumes the entire space available using match_parent in both dimensions. --> <FrameLayout android:id="@+id/content_frame" android:layout_width="match_parent" android:layout_height="match_parent" /> <!-- android:layout_gravity="start" tells DrawerLayout to treat this as a sliding drawer on the left side for left-to-right languages and on the right side for right-to-left languages. The drawer is given a fixed width in dp and extends the full height of the container. A solid background is used for contrast with the content view. -->
<ListView android:layout_weight="1" android:id="@+id/left_drawer" android:layout_width="240dp" android:layout_height="fill_parent" android:layout_gravity="start" android:choiceMode="singleChoice" android:divider="@android:color/transparent" android:dividerHeight="1dp" android:background="@color/Black"/> </android.support.v4.widget.DrawerLayout>

3 个答案:

答案 0 :(得分:0)

您可以将此库用于您的实施:From GITHUB

它允许您将自定义布局放在滑动菜单中。

谢谢,

答案 1 :(得分:0)

这是解决方案:

在项目中

创建一个新的xml文件。并在xml viewer中粘贴下面的代码。之后在图形视图中,您可以看到自定义列表视图。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="48dp" 
android:background="@drawable/list_selector">

<ImageView
    android:id="@+id/icon"
    android:layout_width="25dp"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="12dp"
    android:layout_marginRight="12dp"
    android:contentDescription="@string/desc_list_item_icon"
    android:src="@drawable/ic_home"
    android:layout_centerVertical="true" />

<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_toRightOf="@id/icon"
    android:minHeight="?android:attr/listPreferredItemHeightSmall"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:textColor="@color/list_item_title"
    android:gravity="center_vertical"
    android:paddingRight="40dp"/>

<TextView android:id="@+id/counter"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/counter_bg"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:layout_marginRight="8dp"
    android:textColor="@color/counter_text_color"/>

并转到列表视图适配器[因为在您的导航栏中使用列表视图来显示菜单项。]在gat视图方法中更改新创建的布局的布局[我们现在创建的列表项]

这是我的获取视图方法。它可能与您的getview方法不同。但我想这会对您有帮助。

public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        LayoutInflater mInflater = (LayoutInflater)
                context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = mInflater.inflate(R.layout.drawer_list_item_old, null);
    }

    ImageView imgIcon = (ImageView) convertView.findViewById(R.id.icon);
    TextView txtTitle = (TextView) convertView.findViewById(R.id.title);
    TextView txtCount = (TextView) convertView.findViewById(R.id.counter);

    imgIcon.setImageResource(navDrawerItems.get(position).getIcon());        
    txtTitle.setText(navDrawerItems.get(position).getTitle());

    // displaying count
    // check whether it set visible or not
    if(navDrawerItems.get(position).getCounterVisibility()){
        txtCount.setText(navDrawerItems.get(position).getCount());
    }else{
        // hide the counter view
        txtCount.setVisibility(View.GONE);
    }

    return convertView;
}

答案 2 :(得分:0)

您必须使用自定义布局创建自定义Adapter类,并将该适配器设置为列表视图。检查问题并回答here