ITeye博客

Android AlertDialog动态添加组件和padding靠边问题

fonter · 2010-08-09 11:30 · android · 移动开发 · Markdown

先上图

 

 

 

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>

评论(3)

  1. #3 楼 · rocbond · 2012-02-07 11:08

    顶一个,,,解决了我的大问题。。。

  2. #2 楼 · fonter · 2010-12-06 08:49

    松鼠88 writes:

    请问在AlertDialog的每个条目中加个图片改怎么弄啊,楼主?

    你可能参考http://fonter.iteye.com/blog/674971这个。。。

  3. #1 楼 · 松鼠88 · 2010-12-05 22:47

    请问在AlertDialog的每个条目中加个图片改怎么弄啊,楼主?

评论功能维护中,当前仅展示历史评论。