起初我將創建一個列表佈局...是這樣的:
list_layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#b5b5b5"
android:dividerHeight="1dp" />
</LinearLayout>
然後進行簡單的TextView,如:
single_item_layout
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/singleItem"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:textStyle="bold"
android:textSize="22dp"
android:textColor="#FFFFFF"
android:padding="10dp"
android:text="blue thingy"
android:background="#336699" />
</LinearLayout>
和一個簡單的主要佈局:
主要
<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" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:onClick="popUp"
android:text="pop dialog list" />
</RelativeLayout>
最後一個簡單的主要活動:
import java.util.ArrayList;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void popUp(View v){
// Dummy list:
ArrayList<String> dummies = new ArrayList<String>();
dummies.add("dumm1");
dummies.add("dumm2");
dummies.add("dumm3");
dummies.add("dumm4");
dummies.add("dumm5");
final Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.list_layout);
dialog.setTitle("List Title");
ListView listView = (ListView) dialog.findViewById(R.id.list);
ArrayAdapter<String> ad = new ArrayAdapter<String>(this, R.layout.single_item_layout , R.id.singleItem, dummies);
listView.setAdapter(ad);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
//do something on click
dialog.dismiss();
}
});
dialog.show();
}
}
,這一切。
我試圖儘可能簡單,你可以谷歌的想法,使您的列表有點吸引力,如here。
請把你的佈局粘貼到這裏:) – erbsman 2012-03-27 20:49:00