Android 实现微信聊天一样的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >
    <TextView android:id="@+id/text"
    	android:paddingTop="10dip"
    	android:textColor="#FFA07A"
    	android:background="@drawable/bg_normal"
    	android:layout_width="wrap_content"
    	android:layout_height="wrap_content"/>
</LinearLayout>

布局文件如下:

一个是主程序布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingTop="10dip"
    android:paddingLeft="30dip"
    android:paddingRight="30dip"
    >
    <ListView android:id="@+id/listview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:clickable="false"
    android:scrollbars="none">
    </ListView>
</LinearLayout>

另一个是listview的子view的布局

主程序代码如下:

package com.cloay;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
/**
 * 
 * MyListTestActivity.java
 * @author cloay
 * 2011-10-18
 */
public class TestActivity extends Activity {
    /** Called when the activity is first created. */
	private ListView listView = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initView();
    }

	private void initView() {
		listView = (ListView)findViewById(R.id.listview);
        listView.setDividerHeight(0);
        ListAdapter adapter = new ListAdapter(this, 5);
        listView.setAdapter(adapter);
	}
    
    public class ListAdapter extends BaseAdapter {
    	private LayoutInflater inflater;
    	private int size;
    	public ListAdapter(Context context, int size){
    		this.size = size;
    		inflater = LayoutInflater.from(context);
    	}
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return size;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			convertView = inflater.inflate(R.layout.list, null);
			TextView text = (TextView) convertView.findViewById(R.id.text);
			text.setText(position + ":This is a test !dasgagaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaafdfffffdffg");
			convertView.setEnabled(false);
			return convertView;
		}
    }
}
运行界面如下

说明:转载请注明出处

阅读更多
个人分类: android
上一篇android之ImageSwitcher 图片查看
下一篇Android SharedPreferences的使用
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭