在上一篇文章中,我们已经实现在listview显示数据库内容的。但是我们listview中,排版不是很好看,所以这篇文章呢,我们来对listveiw进行美化。哈哈,说白了,就是对listview添加一个布局文件,然后把布局文件转化为view对象显示出来。

这里代码我们不贴全部,就贴上新增加的布局文件和有做修改的方法。

先看图片吧。

然后看listview的布局文件,其实从这个例子,大家可以知道,listview的项目可以显示很复杂的东西,只要是view对象都是可以显示的额,只要你的布局文件写好了。都可以。

list_layout.xml

<?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="match_parent" > <TextView
android:textSize="20sp"
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="名字" /> <LinearLayout
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" > <TextView
android:id="@+id/age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="年龄" /> <TextView
android:id="@+id/phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="手机" />
</LinearLayout> </RelativeLayout>

MainActivity.java  这里只贴出修改的地方,其实也就是修改了 getView的几条语句而已。这里写出 getView方法的内容

public View getView(int position, View convertView, ViewGroup parent) {
Girl girl = girList.get(position);
/* TextView tv = new TextView(MainActivity.this);
tv.setTextSize(18);
//获取集合中的元素
tv.setText(girl.toString());*/
View v ;
/*
* 做判断是为了listview的性能优化,converView表示一个缓冲区域。
* 在看过的条目,离开屏幕后,其实会被缓冲起来,所以我们没必要
* 每一次都去 填充,可以先判断是否有缓冲
*
* */
if(convertView ==null){
//把ListView的布局文件填充为View对象
v = View.inflate(MainActivity.this, R.layout.list_layout, null);
}else{
v = convertView ;
}
/*v.findViewById(R.id.name);前面加v是因为找的布局文件元素是listView对象的
* 而v加载的布局文件就是listview的布局文件
*/
TextView name = (TextView)v.findViewById(R.id.name);
name.setText(girl.getName());
TextView age = (TextView) v.findViewById(R.id.age);
age.setText(girl.getAge()+"");
TextView phone = (TextView)v.findViewById(R.id.phone);
phone.setText(girl.getPhone());
return v;
}

最新文章

  1. 【分布式】Zookeeper使用--开源客户端
  2. python中使用heapq查看最大与最小的N个元素列表
  3. MVC中的时间js格式化
  4. ModernUI教程:定义一个Logo
  5. JSP基本面试的试题
  6. iOS - AVPlayer 音视频播放
  7. [AHOI 2006][BZOJ 1269]文本编辑器editor
  8. [课程相关]homework-09
  9. nORA-01000: 超出打开游标的最大数(SDE连接)
  10. Singleton(单例)模式
  11. 201521123095 《Java程序设计》第9周学习总结
  12. [LeetCode] Valid Square 验证正方形
  13. DSAPI.网络.网卡信息属性表
  14. selenium采用find_element_by方法识别页面元素
  15. 微信小程序之微信登陆 —— 微信小程序教程系列(20)
  16. 截取所有的winform runtime error
  17. Jquery ajax, Axios, Fetch区别之我见(转载)
  18. 第十四节,卷积神经网络之经典网络Inception(四)
  19. 普通函数跟箭头函数中this的指向问题
  20. 16款优秀的Vue UI组件库推荐

热门文章

  1. img标签显示本地文件
  2. wpf设置字体颜色渐变和字体阴影
  3. vs plug
  4. 时间服务器: NTP 服务器及客户端搭建
  5. hdu 1848 Fibonacci again and again(sg)
  6. Eclipse中Maven的本地仓库引导配置
  7. JavaScript中的数组和对象 增删遍
  8. yeomen/bower/grunt
  9. L118
  10. Codeforces Round #254(div2)A