Android 警报对话框列表

标签 android android-alertdialog

我正在创建一个带有项目列表的AlertDialog。 当前显示的图像:

AlertDialog

我想让类别列表项以粗体显示。 有办法做到这一点吗?

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Make a choice from the list:");
builder.setCancelable(false);
builder.setItems(R.array.items_array, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int item) {
        Toast.makeText(getApplicationContext(), "Selection: " + item, Toast.LENGTH_SHORT).show();
    }
});
AlertDialog alert = builder.create();
alert.show();

最佳答案

即使您只想要标题,我也会使用 ExpandableListView。对于我的一个项目,我遇到了涉及 header 的完全相同的问题,我必须为其创建一个自定义适配器。

首先创建一个新类,其中包含...

    public class MyCustomListAdapter extends BaseExpandableListAdapter 

这些是我的项目中的“getChildView”和“getGroupView”...

@Override
public View getChildView(int groupPosition, int childPosition,
        boolean isLastChild, View view, ViewGroup parent) {

    DetailInfo detailInfo = (DetailInfo) getChild(groupPosition,
            childPosition);
    if (view == null) {
        LayoutInflater infalInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = infalInflater.inflate(R.layout.child_row, null);
    }
    TextView childItem = (TextView) view.findViewById(R.id.childItem);
    childItem.setText(detailInfo.getName().trim());

    return view;
}

@Override
public View getGroupView(int groupPosition, boolean isLastChild, View view,
        ViewGroup parent) {

    HeaderInfo headerInfo = (HeaderInfo) getGroup(groupPosition);
    if (view == null) {
        LayoutInflater inf = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inf.inflate(R.layout.group_heading, null);
    }

    TextView heading = (TextView) view.findViewById(R.id.heading);
    heading.setText(headerInfo.getName().trim());

    return view;
}

现在,您可以完全控制显示为组或子项的内容。在这种情况下,组显示为标题,子项显示为数据。

这是我的 ListView 使用此代码的屏幕截图......

ScreenShot

关于Android 警报对话框列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20927295/

相关文章:

android - 在 Android 中,如何像在 Google map 中那样显示带有方角的警告对话框?

android - 具有自定义 View 的 AlertDialog.Builder : how to initialize the view?

android - 无法让 Android DatePickerDialog 切换到 Spinner 模式

android - 检查设备是否支持 Adob​​e Air

android - 我可以为两个不同的应用程序使用相同的 Firebase 吗?

java - 未找到原生 UnsatisfiedLinkError 异常 Android JNI 的实现

java - 线程和中断: Continue or exit?

android - 如何在 list 中设置 UP Activity

android - 选择微调项时应用程序崩溃?

android - AlertDialog 与 Edittext 作为自定义标题 View