Ran*_*ku' 111 android layout-inflater android-alertdialog
Avoid passing null as the view root当用nullas 膨胀视图时,我得到了lint警告parent,例如:
LayoutInflater.from(context).inflate(R.layout.dialog_edit, null);
Run Code Online (Sandbox Code Playgroud)
但是,视图是用作一个内容AlertDialog,使用setViewon AlertDialog.Builder,所以我不知道应该传递什么parent.
parent在这种情况下你认为应该是什么?
Edw*_*rey 156
使用此代码在没有警告的情况下膨胀对话框视图:
View.inflate(context, R.layout.dialog_edit, null);
Run Code Online (Sandbox Code Playgroud)
eme*_*sso 33
简短的故事是,当您为对话框充气视图时,parent应为null,因为在View通胀时间不知道.在这种情况下,您有三个基本解决方案来避免警告:
inflate(int resource, ViewGroup root, boolean attachToRoot).设置attachToRoot为false.这告诉inflater父母不可用.请查看http://www.doubleencore.com/2013/05/layout-inflation-as-intended/,以便对此问题进行深入讨论,特别是最后的"每个规则都有例外"部分.
小智 17
在ViewGroup解决警告时抛出null:
View dialogView = li.inflate(R.layout.input_layout,(ViewGroup)null);
Run Code Online (Sandbox Code Playgroud)
其中li是LayoutInflater's对象.
Jef*_*hen 14
你应该使用AlertDialog.Builder.setView(your_layout_id),所以你不需要给它充气.
AlertDialog.findViewById(your_view_id)创建对话框后使用.
使用(AlertDialog) dialogInterface来获取dialog里面的OnClickListener,然后dialog.findViewById(your_view_id).
如果你真的没有parent(例如创建视图AlertDialog),除了传递之外别无选择null.所以这样做是为了避免警告:
final ViewGroup nullParent = null;
convertView = infalInflater.inflate(R.layout.list_item, nullParent);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
37594 次 |
| 最近记录: |