农村四月闲人少,勤学苦攻把名扬。这篇文章主要讲述android的ListView图文混搭相关的知识,希望能为你提供帮助。
main4.xml
< ?xml version="1.0" encoding="utf-8"?> < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > < ListView android:id="@+id/listView4_1_1" android:layout_width="match_parent" android:layout_height="wrap_content" > < /ListView> < /LinearLayout>
main4_item.xml
< ?xml version="1.0" encoding="utf-8"?> < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center" > < ImageView android:id="@+id/imageView4_1_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="https://www.songbingjia.com/android/@drawable/ic_launcher" /> < TextView android:id="@+id/textView4_1_1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="textView" /> < /LinearLayout>
MainActivity4
package com.example.listview; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.R.anim; import android.R.drawable; import android.app.Activity; import android.os.Bundle; import android.widget.ListView; import android.widget.SimpleAdapter; public class MainActivity4 extends Activity{ private ListView listView; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.main4); listView=(ListView) findViewById(R.id.listView4_1_1); //一个列表项的内容 Map< String, Object> map=new HashMap< String, Object> (); map.put("image", R.drawable.ic_launcher); map.put("name", "张三"); Map< String, Object> map2=new HashMap< String, Object> (); map2.put("image", R.drawable.ic_launcher); map2.put("name", "王五"); List< Map< String, Object> > list=new ArrayList< Map< String,Object> > (); list.add(map); list.add(map2); SimpleAdapter simpleAdapter=new SimpleAdapter(this, list, R.layout.main4_item, new String [] {"image","name"}, new int [] {R.id.imageView4_1_1,R.id.textView4_1_1}); listView.setAdapter(simpleAdapter); } }
文章图片
【android的ListView图文混搭】亦可以自定义Adapter
mian5.xml
< ?xml version="1.0" encoding="utf-8"?> < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > < ListView android:id="@+id/listView5_1_1" android:layout_width="match_parent" android:layout_height="wrap_content" > < /ListView> < /LinearLayout>
main5_item.xml
< ?xml version="1.0" encoding="utf-8"?> < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center" > < ImageView android:id="@+id/imageView5_1_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="https://www.songbingjia.com/android/@drawable/a" /> < TextView android:id="@+id/textView5_1_1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="textView" /> < /LinearLayout>
MainActivity5
package com.example.listview; import android.app.Activity; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; public class MainActivity5 extends Activity { private ListView listView; //定义的数据 private int [] images={R.drawable.ic_launcher,R.drawable.ic_launcher}; private String [] names={"xiaobai","xiaoming"}; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.main5); listView=(ListView) findViewById(R.id.listView5_1_1); listView.setAdapter(new MyAdapter()); } //自定义适配器 class MyAdapter extends BaseAdapter{ //获取列表项总数 @Override public int getCount() { // TODO Auto-generated method stub return names.length; } //获取每一个列表项(根据下标获取) @Override public Object getItem(int position) { // TODO Auto-generated method stub return names[position]; } //获取每一个列表项的id @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } // @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub //获取布局填充器 LayoutInflater layoutInflater=getLayoutInflater(); //获取main5_item的布局模板 View view=layoutInflater.inflate(R.layout.main5_item, null); //从得到的view查找相应view ImageView imageView=(ImageView) view.findViewById(R.id.imageView5_1_1); TextView textView=(TextView) view.findViewById(R.id.textView5_1_1); imageView.setImageResource(images[position]); textView.setText(names[position]); return view; } } }
文章图片
文章图片
这种方式会多次调用getView()方法
本文出自 “matengbing” 博客,请务必保留此出处http://matengbing.blog.51cto.com/11395502/1882233
推荐阅读
- Android的ListView——适配器模式
- android将图片和文字水平排列
- ArcGIS for android添加图层几何体
- Android Fragment
- Android中使用ListView实现分页刷新(线程休眠模拟)(滑动加载列表)
- QT开发安卓APP的中文字体问题
- Android 秒级编译 Freeline
- Android JNI编程——JNI概念以及C语言Dev-C++开发环境搭建编写HelloWorld
- Android JNI编程——C语言的基本数据类型,输出函数,输入函数