列表视图与标题,图像和文本?

Dan*_*vey 8 android listview android-layout android-xml

我需要制作一个列表视图,左边有一个图像,该图像右侧有一个标题,下面有一个描述.我已经检查了文档中的列表视图,但我似乎无法找到这样的示例.我有一个XML定义的选项卡视图(根据android示例tabs1),列表视图作为第一个选项卡内容.但是我想通过RSS新闻源将内容添加到代码中的listview.

我正在考虑使用html注入字符串使用webview,但我如何从drawable文件夹中插入图像.(这些图像只是本地存储的"新闻图标",而不是来自互联网的图像)

对不起,如果这是一个小小的问题,但任何帮助赞赏:)

kgi*_*kis 10

我想你正在描述的结构是指ListView项的内容.您可以通过为单个项目定义布局来实现此目的.以下代码在类似情况下对我有用:

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:padding="6dip" android:layout_height="?android:attr/listPreferredItemHeight">
    <ImageView
        android:id="@+id/result_icon"        
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:layout_marginRight="6dip"        
        android:src="@drawable/image1"/>
    <TextView
        android:id="@+id/result_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"        
        android:layout_toRightOf="@id/result_icon"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"        
        android:layout_alignWithParentIfMissing="true"                
        android:gravity="center_vertical"
        android:text="Title" />
    <TextView  
        android:id="@+id/result_second_line"
        android:layout_width="fill_parent"
        android:layout_height="26dip"      
        android:layout_toRightOf="@id/result_icon"
        android:layout_below="@id/result_name"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"        
        android:singleLine="true"
        android:ellipsize="marquee"
        android:text="Second line" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

然后,您需要扩展BaseAdapter以与ListActivity一起使用.您需要对布局进行膨胀,并使用RSS源中的数据填充它.

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    Result result = this.results.get(position);


    LayoutInflater inflater = (LayoutInflater) context.getSystemService(
                                        Context.LAYOUT_INFLATER_SERVICE);
    RelativeLayout view = (RelativeLayout) 
            inflater.inflate(R.layout.result_item, null, false);

    ImageView image = (ImageView) view.findViewById(R.id.result_icon);
    image.setImageResource(result.imageResource);

    TextView name = (TextView) view.findViewById(R.id.result_name);
    name.setText(result.location);

    TextView secondLine = (TextView) view.findViewById(R.id.result_second_line);
    secondLine.setText(result.shortDescription);

    return view;
}
Run Code Online (Sandbox Code Playgroud)