顶部的RelativeLayout按钮和按钮下方的自定义列表视图

时间:2012-01-04 23:02:57

标签: android android-layout button relativelayout

我搜索过但没有找到任何我想做的例子。我确定它是微不足道的,但我很难过。我有一个自定义列表视图适配器和一个已定义的相对布局。为每个项目调用整个布局,我希望我的按钮只出现在顶部。任何建议都将不胜感激。

现在的样子:

http://img440.imageshack.us/img440/2803/device20120104173817.png

这就是我想要的样子:

http://img215.imageshack.us/img215/805/devicewant.png

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

<RelativeLayout 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content"
        android:id="@+id/relativeLayout2">

    <TextView
        android:id="@+id/postinfo"
        android:layout_below="@+id/FirstPage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusable="false" />

    <TextView
        android:id="@+id/dateinfo"
        android:layout_alignRight="@+id/postinfo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusable="false" />

        <TextView
        android:id="@+id/post"
        android:layout_below="@+id/postinfo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusable="false" />

            <ImageView
        android:id="@+id/icon2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/post"
        android:contentDescription="OCForums thread icon"
        android:focusable="false"
         />

            <RelativeLayout
                android:id="@+id/relativeLayout3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="top" >

                <Button
                    android:id="@+id/LastPage"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentTop="true"
                    android:text="@string/last" />

                <Button
                    android:id="@+id/NextPage"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    android:layout_toLeftOf="@+id/LastPage"
                    android:text="@string/next" />

                <Button
                    android:id="@+id/FirstPage"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentTop="true"
                    android:text="@string/first" />

                <Button
                    android:id="@+id/PreviousPage"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    android:layout_toRightOf="@+id/FirstPage"
                    android:text="@string/previous" />

            </RelativeLayout>

        </RelativeLayout>

</RelativeLayout>

静态类MyCustomAdapter2扩展BaseAdapter {     private LayoutInflater mInflater;

public MyCustomAdapter2(Context context, int textViewResourceId,List<List<String>> objects) {
    super();
    mInflater = LayoutInflater.from(context);
    rid = textViewResourceId;
    fcontext = context;
    usernames = objects.get(0);
    usernamecolors = objects.get(1); 
    dates = objects.get(2);
    messages = objects.get(3);
    Log.i("is it empy?(messages)",Integer.toString(messages.size()));
    Log.i("is it empy?(dates)",Integer.toString(dates.size()));
    Log.i("is it empy?(usernamecolors)",Integer.toString(usernamecolors.size()));
    Log.i("is it empy?(usernames)",Integer.toString(usernames.size()));
    }

public int getCount() {
return lists.size();
}

public Object getItem(int position) {
return position;
}

public long getItemId(int position) {
return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(rid, null);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.postinfo);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(usernames.get(position));
holder.text.setTextColor(Color.parseColor(usernamecolors.get(position)));

ViewHolder holderd;
if (convertView == null) {
convertView = mInflater.inflate(rid, null);
holderd = new ViewHolder();
holderd.text = (TextView) convertView.findViewById(R.id.dateinfo);
convertView.setTag(holderd);
} else {
holderd = (ViewHolder) convertView.getTag();
}
holderd.text.setText(dates.get(position));


ViewHolder holderm;
if (convertView == null) {
convertView = mInflater.inflate(rid, null);
holderm = new ViewHolder();
holderm.text = (TextView) convertView.findViewById(R.id.post);
convertView.setTag(holderm);
} else {
holderm = (ViewHolder) convertView.getTag();
}
holderm.text.setText(messages.get(position));


    ImageView icon=(ImageView)convertView.findViewById(R.id.icon2);

    if (newlist.size() > 0 && lists.get(position).matches(newlist.get(position))){
    icon.setImageResource(R.drawable.forum_new);
    }
    else{
    icon.setImageResource(R.drawable.forum_old);
    }

return convertView;
}

static class ViewHolder {
TextView text;
}
}

这就是我所说的全部。

ListView listview = (ListView) findViewById(R.id.listView3);
            listview.setAdapter(new CustomListView.MyCustomAdapter2(ThreadActivity.this, R.layout.row2, output));

1 个答案:

答案 0 :(得分:1)

您需要将标题定义为单独的布局(单独的xml文件),然后实例化它并将其作为标题添加到列表视图中 - 然后此布局将显示为列表中的第一项。

请参阅此问题:Android ListView with complex header