将最后一项放在列表视图的底部

时间:2013-09-01 10:20:48

标签: android xml android-layout listview android-listview

我正在使用sidemenu listview,每个项目都是这个xml“rbm_item.xml”:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="@dimen/rbm_item_height"
android:orientation="horizontal"
android:paddingTop="@dimen/rbm_item_padding_topbottom"
android:paddingBottom="@dimen/rbm_item_padding_topbottom"
android:paddingLeft="@dimen/rbm_item_padding_leftright"
android:paddingRight="@dimen/rbm_item_padding_leftright"    >

<ImageView
    android:id="@+id/rbm_item_icon"
    android:layout_height="@dimen/rbm_item_image_height"
    android:layout_width="@dimen/rbm_item_image_width"
    android:scaleType="fitCenter"
     />

<TextView         
    android:id="@+id/rbm_item_text"
    android:layout_width="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_height="wrap_content"
    android:textSize="@dimen/rbm_item_text_size"
    android:textColor="@color/rbm_item_text_color"
    android:paddingLeft="@dimen/rbm_item_text_padding_left"
    android:paddingTop="@dimen/rbm_item_text_padding_top"
    android:singleLine="true"
    android:ellipsize="end"/>

<TextView         
    android:id="@+id/rbm_item_text_sub"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/rbm_item_text"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:textSize="@dimen/rbm_item_text_sub_size"
    android:textColor="@color/rbm_item_text_sub_color"
    android:paddingLeft="@dimen/rbm_item_text_sub_padding_left"
    android:singleLine="true"
    android:ellipsize="end"/>

我为这个listview项目制作了一个适配器:

    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(
                R.layout.rbm_item, null);
        }

        ImageView icon = (ImageView) convertView.findViewById(R.id.rbm_item_icon);
        icon.setImageResource(getItem(position).iconRes);

        TextView title = (TextView) convertView.findViewById(R.id.rbm_item_text);
        title.setText(getItem(position).tag);

        TextView Description = (TextView) convertView
            .findViewById(R.id.rbm_item_text_sub);


        return convertView;
    }

,这就是列表xml文件“list.xml”:

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@color/rbm_divider_color"
android:dividerHeight="@dimen/rbm_divider_ht"
android:paddingLeft="@dimen/list_padding"
android:paddingRight="@dimen/list_padding" />

onCreateView函数:

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    return inflater.inflate(R.layout.list, null);
    }

我通过此函数将项目动态添加到适配器adapter.add(...)

    public void parseXml(int menu) {
    try {
        adapter = new SampleAdapter(getActivity());
        XmlResourceParser xpp = getResources().getXml(menu);
        xpp.next();
        int eventType = xpp.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT) {
            if (eventType == XmlPullParser.START_TAG) {
                String elemName = xpp.getName();
                if (elemName.equals("item")) {
                    String textId = xpp.getAttributeValue(
                        "http://schemas.android.com/apk/res/android", "title");
                    String iconId = xpp.getAttributeValue(
                        "http://schemas.android.com/apk/res/android", "icon");
                    String resId = xpp.getAttributeValue(
                        "http://schemas.android.com/apk/res/android", "id");
                    String Desc = MainActivity.getItemDescription(resId);

                    //Log.i("", "parsing :" + Desc);
                    this.adapter.add(new SampleItem(textId, Integer.valueOf(iconId
                        .replace("@", "")), Desc));

                }

            }

            eventType = xpp.next();

        }

    } catch (Exception e) {
        Log.e("log", "while parsing xml", e);
    }

    setListAdapter(adapter);
}

SampleItem是包含图标,标题...属性的简单结构,而Menu参数是包含<menu>标记内的项目的xml文件,我在它们上面和它们上面都有它们就像:

  <item android:id="@+id/ribbon_menu_profile" android:title="Profile" 
     android:icon="@drawable/profile"></item>
 .....

我真正想要的是将这些项目的最后一项放在列表的底部

我搜索了很多但是如果我想通过getView函数中的convertView参数设置它,我没有找到任何View类方法。

有没有办法做到这一点?

所以我想要这样的事情:

enter image description here

2 个答案:

答案 0 :(得分:0)

试  listview.addFooterView(视图);

答案 1 :(得分:0)

你可以这样做:只需将你的列表放在relativeLayout中,然后在下面添加一个额外的项目:

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/lastStickyItem"    
android:layout_width="match_parent"
android:layout_height="@dimen/rbm_item_height"
android:orientation="horizontal"
android:paddingTop="@dimen/rbm_item_padding_topbottom"
android:paddingBottom="@dimen/rbm_item_padding_topbottom"
android:paddingLeft="@dimen/rbm_item_padding_leftright"
android:paddingRight="@dimen/rbm_item_padding_leftright"    >

<ImageView
android:id="@+id/rbm_item_icon"
android:layout_height="@dimen/rbm_item_image_height"
android:layout_width="@dimen/rbm_item_image_width"
android:scaleType="fitCenter"
 />

<TextView         
android:id="@+id/rbm_item_text"
android:layout_width="wrap_content"
android:layout_alignParentTop="true"
android:layout_height="wrap_content"
android:textSize="@dimen/rbm_item_text_size"
android:textColor="@color/rbm_item_text_color"
android:paddingLeft="@dimen/rbm_item_text_padding_left"
android:paddingTop="@dimen/rbm_item_text_padding_top"
android:singleLine="true"
android:ellipsize="end"/>

<TextView         
android:id="@+id/rbm_item_text_sub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/rbm_item_text"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:textSize="@dimen/rbm_item_text_sub_size"
android:textColor="@color/rbm_item_text_sub_color"
android:paddingLeft="@dimen/rbm_item_text_sub_padding_left"
android:singleLine="true"
android:ellipsize="end"/>
</RelativeLayout>

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@id/lastStickyItem"
android:divider="@color/rbm_divider_color"
android:dividerHeight="@dimen/rbm_divider_ht"
android:paddingLeft="@dimen/list_padding"
android:paddingRight="@dimen/list_padding" />

</RelativeLayout>

填写活动内的额外(最后)项而不是适配器......