這是我做的。
row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<TextView
android:id="@+id/left_text"
android:layout_width="75dip"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/right_text"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:maxLines="2"
android:ellipsize="end" />
</LinearLayout>
主要應用
public class Main extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
List<Datum> data = new ArrayList<Datum>();
data.add(new Datum("FIRST", "One line of text."));
data.add(new Datum("SECOND", "Two lines of text. Two lines of text. Two lines of text."));
data.add(new Datum("THIRD", "Three or more lines of text. Three or more lines of text. Three or more lines of text. Three or more lines of text. Three or more lines of text. Three or more lines of text."));
data.add(new Datum("FOURTH", "One line of text, again."));
ListView leftList = (ListView) findViewById(R.id.my_list);
leftList.setAdapter(new MyAdapter(this, R.layout.row, data));
}
}
class Datum
{
String left, right;
Datum(String left, String right)
{
this.left = left;
this.right = right;
}
}
class MyAdapter extends ArrayAdapter<Datum>
{
List<Datum> data;
int textViewResourceId;
MyAdapter(Context context, int textViewResourceId, List<Datum> data)
{
super(context, textViewResourceId, data);
this.data = data;
this.textViewResourceId = textViewResourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
if (convertView == null)
{
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(textViewResourceId, null);
}
Datum datum = data.get(position);
if (datum != null)
{
TextView leftView = (TextView) convertView.findViewById(R.id.left_text);
TextView rightView = (TextView) convertView.findViewById(R.id.right_text);
if (leftView != null)
{
leftView.setText(datum.left);
}
if (rightView != null)
{
rightView.setText(datum.right);
}
}
return convertView;
}
}

你一定要使用自定義列表視圖項與自定義適配器,如果你有很多的動態行。 – bigstones 2011-04-23 22:29:21