0

我正在创建一个在线聊天应用程序。我正在使用列表视图来显示当时在线的联系人。我的问题是我想在一行中显示联系人姓名和绿色状态图标或类似的东西(就像在 facebook 或任何其他聊天应用程序中一样)。

我不知道如何在列表视图的右端显示联系人姓名+绿色,有人可以帮我解决问题吗

4

7 回答 7

1

你需要的是一个从BaseAdapter. 在那里,您可以为每个listItem.

因此,首先您必须定义一个布局,其中包含TextView联系人姓名的 a 和例如 a ImageView

此布局应在getView()您的BaseAdapter. 但是请阅读这篇文章以确保您的“平滑滚动”listView

于 2013-09-24T06:13:07.960 回答
0

您需要为列表视图创建自定义适配器并将其设置为此列表视图。

教程

将向您展示如何做到这一点。

祝你好运

于 2013-09-24T06:14:21.597 回答
0

为此,您必须为您的ListView项目进行自定义布局,如下所示:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
    >

<TextView
        android:text="Contact name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
    />
<ImageView
        android:layout_width="10dip"
        android:layout_height="match_parent"
        android:background="green_circle_image"
        android:layout_alignParentRight="true"

    />

</RelativeLayout>
于 2013-09-24T06:14:56.350 回答
0
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="horizontal"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:gravity="center_vertical"
        >

    <TextView
            android:text="Contact name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
        />
    <View
            android:layout_width="10dip"
            android:layout_height="match_parent"
            android:background="@color/green"

        />

</LinearLayout>
于 2013-09-24T06:10:42.687 回答
0

看这是一个非常简单的案例,您希望在 a 中添加一个自定义项listview,而不是给您代码,我建议您了解布局膨胀的概念,您可以使用您设计的布局对几乎所有容器布局进行膨胀。

谷歌它,你会在你的案例中找到很多例子,这似乎是textview唯一imageview的。

于 2013-09-24T06:12:34.577 回答
0

尝试这个,

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

<TextView
    android:id="@+id/friendname"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="2.5"
    android:gravity="center_vertical"
    android:paddingLeft="10dip"
    android:singleLine="true"
    android:text="usename" />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0.2"
    android:background="@drawable/profile"
    android:paddingLeft="10dip"
   />

</LinearLayout>
于 2013-09-24T06:13:35.503 回答
0

您可以为自定义适配器使用以下布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/statusTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawableRight="@drawable/status" />

 </LinearLayout>

要动态更改图像,您可以从自定义适配器在 getView(...)中使用setCompoundDrawablesWithIntrinsicBounds (int left, int top, int right, int bottom) 。
前任:

public View getView(int position, View convertView, ViewGroup parent) {
    convertView = context.getLayoutInflater().inflate(R.layout.item_element, null);
    TextView statusTV = (TextView) convertView.findViewById(R.id.status);
    statusTV.setText("...");
    Drawable compoundDrawable = context.getResources().getDrawable(R.drawable.status);
    statusTV.setCompoundDrawables(compoundDrawable, null, null, null);
    return convertView;
}

并使用viewHolder 模式进行 listView 优化。

于 2013-09-24T06:51:36.377 回答