导航抽屉顶部的图像

时间:2014-01-04 14:44:53

标签: android navigation-drawer

如何将图片放在Google Play商店或Google +上的左侧边栏等导航抽屉上?

实际上我有自己的导航抽屉适配器,它为列表视图的每个元素添加了一个图标,这是当前的XML:

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

    <FrameLayout
        android:id="@+id/frame_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </FrameLayout>

    <ListView
        android:id="@+id/left_menu"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/menuBackground"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp" />

</android.support.v4.widget.DrawerLayout>

我想我必须在这个XML中添加ImageView,但我不确定这是否正确。

2 个答案:

答案 0 :(得分:2)

我今天刚刚发现了如何实现这一点(如果你还没有解决这个问题)。

花了我几个小时来提出正确的问题

我认为你看起来像这样

Image on top of the listview

如果是这样,您需要创建一个xml文件作为标题并添加到列表视图的顶部。

所以只需创建一个header.xml

header.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/img_first"
        android:layout_width="fill_parent"
        android:layout_height="200dp"
        android:scaleType="fitXY"
        android:src="@drawable/hatsune" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Text Name"
        android:background="#88f3f3f3"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="20dp"
        android:background="#88f3f3f3"
        android:text="Text Point"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

回到您的活动,请说 Activity.java

//Header of the listview, go to header.xml to customize
    View header = getLayoutInflater().inflate(R.layout.header, null);

  //addHeaderView is to add custom content into first row
    yourListView.addHeaderView(header);
    yourListView.setAdapter(yourAdapter);

答案 1 :(得分:0)

I think I must add ImageView in this XML but I'm not sure if this is the correct way.

不,至少在我个人看来,这不是实施它的正确方法。尝试遵循这样的方法。

  1. 创建要添加到导航抽屉顶部的布局。

  2. 使用getListView().addHeaderView(headerView);

  3. 将该布局添加为列表中的标题

    希望这会有所帮助.. :)

相关问题