1

我通过包含 ImageView 和 TextView 并使用 LayoutInflater 的 xml 文件创建了类似于列表视图的内容。

似乎我以一种非常不正统的方式完成了这项工作,因为我无法弄清楚如何实际设置列表视图的样式(理想情况下,我想定义大小,因为还有其他内容要放在同一界面上)

我已经尝试自己设置LinearLayout的样式,并将LinearLayout放在父LinearLayout中,包括在名为'list'的所述文件中的ListView,尝试在扩展ListActivity的类中定义setContentView。

我希望能够像任何其他控件一样将自定义列表视图控件包含到 LinearLayout 中,而不是控件是整个 UI。

public class ImageActivity extends ArrayAdapter<PlaceStore> {

    private final Context context;
    private final List<PlaceStore> places;

    public ImageActivity(Context context, List<PlaceStore> places) {
        super(context, R.layout.image_text_row, places);
        this.context = context;
        this.places = places;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View rowView = inflater.inflate(R.layout.image_text_row, parent, false);

        TextView textView = (TextView) rowView.findViewById(R.id.label);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.logo);
        textView.setText(places.get(position).getName());

        return rowView;
    }


public class FavouriteActivity extends ListActivity{


    public void onCreate(Bundle savedInstanceState) {

         super.onCreate(savedInstanceState);

         setContentView(R.layout.favourite);

         FavouriteData data = new FavouriteData(this);

         List<PlaceStore> pS = data.returnFileData();

         setListAdapter(new ImageActivity(this, pS));

    }

R.layout.image_text_row

<?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="fill_parent"
    android:padding="5dp" android:background="@drawable/background_type" >


    <ImageView
        android:id="@+id/logo"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="5dp"
        android:src="@drawable/androidmarker" >
    </ImageView>

    <TextView
        android:id="@+id/label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@+id/label"
        android:textSize="20sp" >
    </TextView>

    </LinearLayout>
4

3 回答 3

2

这是一个非常好的教程,确切地说是您正在寻找的

使用自定义 ArrayAdapter 自定义 Android ListView 项

上述教程的输出将与此类似。

Android 自定义列表视图

这肯定会帮助您完成工作。

谢谢 :)。

于 2012-07-22T10:55:04.400 回答
1

试试下面的教程,它将帮助您满足您的要求

自定义 ListView这个

视频 - youtubeVideo

希望能帮助到你。

于 2012-07-22T10:25:45.720 回答
1

我认为您不需要 ListActivity。我建议你这个教程:

列表视图和缓存视图

于 2012-07-22T10:32:54.220 回答