Android AlertDialog动态添加组件和padding靠边问题
2010-09-26 10:05
567 查看
先上图
![](http://dl.javaeye.com/upload/attachment/290116/027fbd12-cd1b-333a-a278-d300ecc0f36e.png)
AlertDialog有个问题是动态添加组件时,组件会很靠边框,这样很不好看,下面这个方案是解决这个问题
![](http://dl.javaeye.com/upload/attachment/290116/027fbd12-cd1b-333a-a278-d300ecc0f36e.png)
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 >
相关文章推荐
- android 动态添加组件出现的一些问题
- android 动态添加组件(RadioGroup 添加RadioButton和其他组件的一些问题)
- Android 利用addView 动态给Activity添加View组件
- Android学习一——动态添加组件与删除,可见性设置,imagebutton简单使用
- 【学习笔记】Android 动态添加控件 getlayoutparams 为空的问题
- Android ListView GridView添加 padding后padding部分不跟随滑动移动问题解决方案
- Android 利用addView 动态给Activity添加View组件
- 解决在BroadcastReceiver(广播接受者)中不能添加AlertDialog(对话框)的问题(android.view.WindowManager$BadTokenException)
- Angular通过指令动态添加组件问题
- Android实战简易教程<十一>(树形组件:ExpandableListView显示和动态添加删除)
- 解决Android 6.0动态添加权限问题
- Android疑难杂症 动态改变Background后Padding无效的问题
- 动态添加ImageView 设置setPadding不起作用问题
- Android 利用addView 动态给Activity添加View组件
- Android中的基础-----如何使用JAVA动态的添加并设置组件属性
- [Android疑难杂症]动态改变Background后Padding无效的问题
- Android GridView之添加分隔线,动态设置高度,实现高度自适应,并解决第一个item不显示的问题
- Android实战简易教程-第十一枪(树形组件:ExpandableListView显示和动态添加删除)
- Android 利用addView 动态给Activity添加View组件
- Android LayoutInflater动态添加子View的属性生效问题