您的位置:首页 > 移动开发 > Android开发

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

2010-09-26 10:05 567 查看
先上图



AlertDialog有个问题是动态添加组件时,组件会很靠边框,这样很不好看,下面这个方案是解决这个问题

1. LayoutInflater mInflater = (LayoutInflater) getContext()
2.         .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
3. View view = mInflater.inflate(R.layout.recordlayout, null );
4. LinearLayout layout = (LinearLayout) view
5.         .findViewById(R.id.id_recordlayout);
6. for  ( int  i =  0 ; i < fieldName.length; i++) {
7.     String name = fieldName[i];
8.     if  ( "_id" .equals(name))
9.         continue ;
10.     TextView tv = new  TextView(getContext());
11.     tv.setText(fieldName[i]);
12.     EditText edit = new  EditText(getContext());
13.     layout.addView(tv);
14.     layout.addView(edit);
15. }
16.
17. AlertDialog.Builder dialog = new  AlertDialog.Builder(getContext());
18. dialog.setTitle(R.string.add_a_record);
19. dialog.setView(view);
20.
21. dialog.setPositiveButton(R.string.ok,
22.         new  DialogInterface.OnClickListener() {
23.             public   void  onClick(DialogInterface dialog,  int  which) {
24.
25.             }
26.         });
27. dialog.setNegativeButton(R.string.cancel,
28.         new  DialogInterface.OnClickListener() {
29.             public   void  onClick(DialogInterface dialog,  int  which) {
30.
31.             }
32.         });
33. dialog.show();


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 >
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐