在警告对话框中膨胀包含listview的布局,可以通过以下几个步骤来实现:
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
public class CustomListAdapter extends BaseAdapter {
private Context context;
private List<String> dataList;
public CustomListAdapter(Context context, List<String> dataList) {
this.context = context;
this.dataList = dataList;
}
@Override
public int getCount() {
return dataList.size();
}
@Override
public Object getItem(int position) {
return dataList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.list_item, parent, false);
}
TextView textView = convertView.findViewById(R.id.textView);
textView.setText(dataList.get(position));
return convertView;
}
}
public class CustomAlertDialog extends AlertDialog {
public CustomAlertDialog(Context context) {
super(context);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_alert_dialog);
ListView listView = findViewById(R.id.listview);
List<String> dataList = new ArrayList<>();
// 填充数据
for (int i = 0; i < 10; i++) {
dataList.add("Item " + i);
}
CustomListAdapter adapter = new CustomListAdapter(getContext(), dataList);
listView.setAdapter(adapter);
}
}
CustomAlertDialog customAlertDialog = new CustomAlertDialog(this);
customAlertDialog.show();
这样,在警告对话框中就可以膨胀包含listview的布局了。
领取专属 10元无门槛券
手把手带您无忧上云