Android - 为列表视图中的每一行添加一个按钮

时间:2012-05-26 08:04:41

标签: android listview button

由于某种原因,我无法查看列表视图的row.xml文件中包含的按钮。

这是我正在使用的main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent">
<ListView 
    android:id="@+id/friendsListView" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"/>
</LinearLayout>

row.xml如下

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

 <ImageView android:id="@+id/imgIcon"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:gravity="center_vertical"
    android:layout_marginRight="10dp"
    android:contentDescription="@string/fbUserPic"/>

 <TextView android:id="@+id/txtTitle"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_vertical"
    android:textStyle="normal"
    android:textSize="12dp"
    android:textColor="#FFFFFF"/>

 <Button android:id="@+id/btnPost"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:text="@string/fbbtnPost"
    android:focusable="false"/>
 </LinearLayout>

以下是我正在使用的自定义适配器中的getView覆盖

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    View row = convertView;
    FriendRowHolder friendHolder = null;

    if (row == null) 
    {
          LayoutInflater inflater = ((Activity)objContext).getLayoutInflater();
          row = inflater.inflate(layoutResourceId, parent, false);

          friendHolder = new FriendRowHolder();
          friendHolder.txtTitle = (TextView) row.findViewById(R.id.txtTitle);
          friendHolder.imgIcon = (ImageView) row.findViewById(R.id.imgIcon);
          friendHolder.btnPost = (Button) row.findViewById(R.id.btnPost);

          row.setTag(friendHolder);
    }
    else
    {
        friendHolder = (FriendRowHolder)row.getTag();
    }

    Friend objFriend = lstFriends.get(position);
    friendHolder.txtTitle.setText(objFriend.GetName());
    friendHolder.imgIcon.setImageBitmap(loadBitmap(objFriend.GetPictureURL()));
    friendHolder.btnPost.setText("Hi");

    return row;
}

1 个答案:

答案 0 :(得分:0)

问题在于按钮被放置在屏幕右侧太多。我只需将按钮移到左侧即可显示。