先上图

AlertDialog有个问题是动态添加组件时,组件会很靠边框,这样很不好看,下面这个方案是解决这个问题
LayoutInflater mInflater = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = mInflater.inflate(R.layout.recordlayout, null);
LinearLayout layout = (LinearLayout) view
.findViewById(R.id.id_recordlayout);
for (int i = 0; i < fieldName.length; i++) {
String name = fieldName[i];
if ("_id".equals(name))
continue;
TextView tv = new TextView(getContext());
tv.setText(fieldName[i]);
EditText edit = new EditText(getContext());
layout.addView(tv);
layout.addView(edit);
}
AlertDialog.Builder dialog = new AlertDialog.Builder(getContext());
dialog.setTitle(R.string.add_a_record);
dialog.setView(view);
dialog.setPositiveButton(R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
dialog.setNegativeButton(R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
dialog.show();
<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:id="@+id/id_recordlayout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:padding="10dip"></LinearLayout> </ScrollView>
本文介绍了一种在Android中使用AlertDialog时,通过自定义视图布局来改善动态添加组件显示效果的方法。通过对视图进行特定设置,使得组件在弹窗内不再紧贴边框,提升了用户体验。
4051

被折叠的 条评论
为什么被折叠?



