使用SimpleAdapter创建ListView
来源:互联网 发布:java style 编辑:程序博客网 时间:2024/05/21 17:21
效果图:
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/TextView1" android:layout_width="match_parent" android:layout_height="50dp" android:gravity="center" android:textSize="20sp" android:text="Hello Guy!"/> <ListView android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="wrap_content" android:divider="#f00" android:dividerHeight="2dp"> </ListView></LinearLayout>
chat_list.xml(这里注意几个layout_width和layout_height属性的使用)
<?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" android:orientation="horizontal" > <ImageView android:id="@+id/icon" android:layout_width="50dp" android:layout_height="50dp"/> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:orientation="vertical"> <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp"/> <TextView android:id="@+id/desc" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="12sp"/> </LinearLayout></LinearLayout>
MainActivity.java
import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import android.app.Activity;import android.os.Bundle;import android.view.Window;import android.widget.ListView;import android.widget.SimpleAdapter;public class MainActivity extends Activity { private ListView chatList; private int[] icons = new int[]{R.drawable.count,R.drawable.download,R.drawable.me, R.drawable.table,R.drawable.write}; private String[] names = new String[]{"像雾又像风","With You","天天酷跑","跟我一起来","lolCome"}; private String[] desc = new String[]{"我是一个安静的美男子。","找个也是会员的小姐姐。","没有人能说我不好。", "我的天呐,我的钱包居然又掉了!!","欢迎来到,王者荣耀!!"}; private List<Map<String, Object>> lists = new ArrayList<Map<String,Object>>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); chatList = (ListView) findViewById(R.id.listview); for(int i = 0; i < names.length; i++){ Map<String, Object> map = new HashMap<String, Object>(); map.put("icon", icons[i]); map.put("name", names[i]); map.put("desc", desc[i]); lists.add(map); } SimpleAdapter sa = new SimpleAdapter(this, lists, R.layout.chat_list, new String[]{"icon","name","desc"}, new int[]{R.id.icon,R.id.name,R.id.desc}); chatList.setAdapter(sa); }}
改变chat_list.xml中ImageView属性:
<ImageView android:id="@+id/icon" android:layout_width="50dp" android:layout_height="50dp" android:paddingLeft="10dp"/>
说明:这里在只写了android:paddingLeft=”10dp”时,会出以下提示
- Consider adding android:paddingStart="10dp" to better support right-to-left layouts - When you define paddingLeft you should probably also define paddingRight for right-to-left
此时的效果图:
为了使界面看上去更和谐,可为右边文字部分加个paddingLeft,此时的效果图:
现在paddingLeft和paddingRight双管齐下:
<ImageView android:id="@+id/icon" android:layout_width="50dp" android:layout_height="50dp" android:paddingLeft="10dp" android:paddingRight="10dp"/>
此时再放效果图:前面的小图标被缩小了
0 0
- 使用SimpleAdapter创建ListView
- ListView-使用SimpleAdapter创建
- 使用SimpleAdapter创建ListView
- 使用SimpleAdapter创建ListView
- 使用SimpleAdapter创建ListView
- 第20章:使用SimpleAdapter创建ListView
- 使用强大的SimpleAdapter创建ListView
- SimpleAdapter创建ListView
- SimpleAdapter创建ListView
- SimpleAdapter创建ListView
- Android ListView使用SimpleAdapter
- 使用SimpleAdapter填充ListView
- ListView -- SimpleAdapter的使用
- 填充ListView(使用SimpleAdapter)
- 使用 SimpleAdapter 显示listview
- ListView使用SimpleAdapter适配器
- 26、使用SimpleAdapter创建ListView和自定义Toast布局显示
- SimpleAdapter与ListView的使用
- linux 网络的配置
- VC++6.0 使用ADO动态链接SQL Server 2005数据库
- 基于matlab开发输出连续值的深度神经网络
- 消息队列
- Linux 命令行里的“瑞士军刀”
- 使用SimpleAdapter创建ListView
- php-pfm.conf
- 结合Wireshark理解TCP连接建立所需的三次握手
- 关于IntelliJ IDEA创建项目无法读取配置文件
- centos7.2下安装php7.1缺少依赖包汇总(初稿)
- Watson Explorer 入门(13):配置中断规则
- 基于windows系统上搭建的android应用集成React native的成功案列
- java——File类
- string 与char* char[]之间的转换