我正在开发一个侧边菜单列表视图,每个项目都是这个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类方法可以做到这一点。
那么有没有什么办法可以做到呢?
所以我想要像这样的东西: