最近在做一个综合性实验,关于短信管理的,里面的短信浏览想做成对话形式的,大概的想法是改变Padding值。
首先是Activity的Layout文件,里面只有一个简单的ListView。
<?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"> <ListView android:id="@+id/main_list" android:layout_width="match_parent" android:layout_height="wrap_content" > </ListView> </LinearLayout>
然后是ListView的Item的布局,也是一个简单的TextView而已。
<?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" > <TextView android:id="@+id/item_txt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="JustForTestJustForTestJustForTestJustForTestJustForTestJustForTestJustForTestJustForTest" android:textColor="@android:color/white" android:textSize="20dp" /> </LinearLayout>
最后是Activity的代码。
package com.raybinlearn.raybin;
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;
public class MainActivity extends Activity
{
private ListView mListView = null;
private int temp = 1;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mListView = (ListView) findViewById(R.id.main_list);
ListAdapter adapter = new ListAdapter(MainActivity.this);
mListView.setAdapter(adapter);
}
/**
* List的一个Item,存放一个文本标签
*
* @author Raybin
*/
class ListItem
{
TextView mtxvName;
ListItem(View parent)
{
mtxvName = (TextView) parent.findViewById(R.id.item_txt);
}
}
/**
* List的内容适配器
*
* @author Raybin
*/
class ListAdapter extends BaseAdapter
{
Context context;
LayoutInflater mInflater;
ListAdapter(Context context)
{
super();
this.context = context;
mInflater = LayoutInflater.from(context);
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2)
{
ListItem listItem = null;
if (arg1 == null)
{
arg1 = mInflater.inflate(R.layout.list_item, null);
listItem = new ListItem(arg1);
arg1.setTag(listItem);
}
else
{
listItem = (ListItem) arg1.getTag();
}
//--改变padding
if (temp%2 == 0)
{
arg1.setPadding(35, 0, 0, 0);
}
else
{
arg1.setPadding(0, 0, 35, 0);
}
temp = temp + 1;
return arg1;
}
@Override
public long getItemId(int arg0)
{
return arg0;
}
@Override
public Object getItem(int arg0)
{
return arg0;
}
@Override
public int getCount()
{
return 10;
}
}
}
效果如下: