原文:http://blog.csdn.net/xyz_lmn/article/details/6906331
这次实现的是像handcent sms或者chomp sms那样的气泡短信样式,也是iphone上的气泡聊天模式。实现这种效果的重点是ListView的divider的设置和背景图片要经过9-patch处理。这样背景图片就会随着文字的添加变化拉伸的区域,而四角的形状不会变化。
主布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/white" > <ListView android:id="@+id/itemLists" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:cacheColorHint="#000000" android:divider="@null" android:listSelector="@android:drawable/list_selector_background" > </ListView> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/item" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/pop" android:gravity="center" android:textColor="@android:color/black" > </TextView>
package com.example.handcentlist; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.widget.ListView; import android.widget.SimpleAdapter; public class HandcenterList extends Activity { private ListView itemList; private List<Map<String, String>> list; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.handcenter_list); //获得ListView的引用 itemList = (ListView)findViewById(R.id.itemLists); //获得需要显示在ListView中的数据 list = getList(); //实现Adapter类 SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.item, new String[]{"item"}, new int[]{R.id.item}); itemList.setAdapter(adapter); itemList.setSelection(0); } /** * 获得显示在ListView中的数据 * @return */ public List<Map<String, String>> getList() { List<Map<String, String>> list = new ArrayList<Map<String,String>>(); Map<String, String> map = new HashMap<String, String>(); map.put("item", "呵呵呵呵呵"); list.add(map); map = new HashMap<String, String>(); map.put("item", "哈哈哈哈"); list.add(map); map = new HashMap<String, String>(); map.put("item", "微微微微i诶诶诶诶"); map = new HashMap<String, String>(); map.put("item", "哈哈哈和哈哈哈和哈哈哈哈哈哈哈哈哈哈哈哈\n呵护哈哈哈和哈哈哈和哈哈哈哈哈哈哈哈哈哈哈哈\n卡卡卡卡快快快快卡卡卡卡"); list.add(map); return list; } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.handcenter_list, menu); return true; } }