开发者

How to customize the list items in an Android AlertDialog

开发者 https://www.devze.com 2023-01-14 02:31 出处:网络
I want to show multiple lines with different text styles for each item in a list using开发者_Go百科 AlertDialog, but can\'t fine any example.

I want to show multiple lines with different text styles for each item in a list using开发者_Go百科 AlertDialog, but can't fine any example.

Does anyone know how to customize the items of an AlertDialog? Or do I really have to create an Activity with a ListView for this?


Use setAdapter() on AlertDialog.Builder to supply your own custom ListAdapter for your list.


edit: by the way, I made some changes to actually show different icons:

list_item.xml:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/icon"
        android:contentDescription="@string/icon_descr"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:layout_gravity="left" />

    <TextView
        android:id="@+id/title"
        android:text=""
        android:paddingLeft="10dip"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

And into your Activity:

// dialog list entries
final String[] items = {
    getString(R.string.entry_0),
    getString(R.string.entry_1),
    getString(R.string.entry_2)
};

// dialog list icons: some examples here
final int[] icons = {
    android.R.drawable.ic_menu_edit,
    android.R.drawable.ic_menu_send,
    android.R.drawable.ic_menu_delete
};

ListAdapter adapter = new ArrayAdapter<String>(
    getApplicationContext(), R.layout.list_item, items) {

    ViewHolder holder;

    class ViewHolder {
        ImageView icon;
        TextView title;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
    final LayoutInflater inflater = (LayoutInflater) getApplicationContext()
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if (convertView == null) {
        convertView = inflater.inflate(
        R.layout.list_item, null);

        holder = new ViewHolder();
        holder.icon = (ImageView) convertView
        .findViewById(R.id.icon);
        holder.title = (TextView) convertView
        .findViewById(R.id.title);
        convertView.setTag(holder);
    } else {
        // view already defined, retrieve view holder
        holder = (ViewHolder) convertView.getTag();
    }     

    holder.title.setText(items[position]);

    holder.icon.setImageResource(icons[position]);
    return convertView;
    }
};

// ----------

AlertDialog.Builder builder = new AlertDialog.Builder(myActivity.this);

builder.setTitle("title");

builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
        // ---
    }

});

builder.create();
if (! ((Activity) myActivity.this).isFinishing()) {
    builder.show();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消