关于ayoutInflater类中inflate用法viewgroup参数的理解

以前在写代码的时候,有一个很不好的习惯,当然这个习惯也是跟着视频教程学的,那就是我们在将xml文件转化成view的时候,会很顺手的将viewpgroup参数写为null。比如下面这个例子。我不想用难的api来实现,我们只用最简单的。在AlertDialog里填充一个edittext的视图

首先是dialog里面的edittext视图

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.       
  7.     <EditText   
  8.         android:background="#C7EDCC"  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:hint="请输入密码">  
  12.           
  13.     </EditText></LinearLayout>  
其次是我们的主布局视图
[html]  view plain copy
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     >  
  6.   
  7.     <Button   
  8.         android:id="@+id/id_showdialog"  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="show dailog" />  
  12.   
  13. </RelativeLayout>  
我们的mainactivity代码:
[java]  view plain copy
  1. package com.derek.demo;  
  2.   
  3. import android.app.AlertDialog;  
  4. import android.os.Bundle;  
  5. import android.support.v7.app.ActionBarActivity;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9.   
  10. public class MainActivity extends ActionBarActivity implements OnClickListener{  
  11.   
  12.     private Button showdialog;  
  13.     private View dialog_item;  
  14.     @Override  
  15.     protected void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.activity_main);  
  18.         showdialog = (Button) findViewById(R.id.id_showdialog);//找到按钮控件  
  19.         showdialog.setOnClickListener(this);//注册点击事件  
  20.     }  
  21.     @Override  
  22.     public void onClick(View v) {  
  23.         switch (v.getId()) {  
  24.         case R.id.id_showdialog:  
  25.             showdialog();//显示AlertDailog  
  26.             break;  
  27.   
  28.         default:  
  29.             break;  
  30.         }  
  31.           
  32.     }  
  33.     private void showdialog() {  
  34.         dialog_item = View.inflate(this, R.layout.dialog_item, null);//加载我们需要的布局,想想我们能不能把它写到onCreate()方法下面呢?  
  35.         AlertDialog.Builder builder = new AlertDialog.Builder(this)//  
  36.         .setTitle("测试")//  
  37.         .setView(dialog_item)//  
  38.         .setNegativeButton("NO",null)//  
  39.         .setPositiveButton("YES"null);  
  40.         AlertDialog dialog = builder.create();  
  41.         dialog.show();  
  42.     }  
  43.   
  44.       
  45. }  
代码是不是很简单?我们来看一下效果图


我们把edittext的高度改为100dp,效果图如下


嗯,貌似实现了我们预想的结果,但事实上是这样吗?我们来看下一个例子,这个例子我参考了其他大神的研究。我们用listview来填充数据,同样,代码类似,我们只是把dialog_item填充到listview中

dialog_item布局如下

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/linearLayout"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:background="#000000"  
  7.     android:orientation="vertical" >  
  8.   
  9.     <EditText  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:background="#C7EDCC"  
  13.         android:hint="请输入密码" >  
  14.     </EditText>  
  15.   
  16. </LinearLayout>  
主布局如下:

[html]  view plain copy
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     >  
  6.   
  7.     <ListView   
  8.         android:id="@+id/id_listview"  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="match_parent"  
  11.          />  
  12. </RelativeLayout>  
mainactivty代码如下:
[java]  view plain copy
  1. package com.derek.demo;  
  2.   
  3. import android.os.Bundle;  
  4. import android.support.v7.app.ActionBarActivity;  
  5. import android.view.LayoutInflater;  
  6. import android.view.View;  
  7. import android.view.ViewGroup;  
  8. import android.widget.BaseAdapter;  
  9. import android.widget.ListView;  
  10.   
  11. public class MainActivity extends ActionBarActivity {  
  12.   
  13.     private ListView listView;  
  14.       
  15.     @Override  
  16.     protected void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.activity_main);  
  19.         listView = (ListView) findViewById(R.id.id_listview);  
  20.         listView.setAdapter(new MyAdapter());  
  21.     }  
  22.   
  23.     class MyAdapter extends BaseAdapter {  
  24.   
  25.         @Override  
  26.         public int getCount() {  
  27.   
  28.             return 10;  
  29.         }  
  30.   
  31.         @Override  
  32.         public Object getItem(int position) {  
  33.   
  34.             return null;  
  35.         }  
  36.   
  37.         @Override  
  38.         public long getItemId(int position) {  
  39.   
  40.             return 0;  
  41.         }  
  42.   
  43.         @Override  
  44.         public View getView(int position, View convertView, ViewGroup parent) {  
  45.             // 我们这里方便起见,不考虑优化,直接用了  
  46.   
  47.             if (convertView == null) {  
  48.                 LayoutInflater inflater = LayoutInflater  
  49.                         .from(MainActivity.this);  
  50.                 convertView = inflater.inflate(R.layout.dialog_item, null);  
  51.             }  
  52.             return convertView;  
  53.         }  
  54.   
  55.     }  
  56.   
  57. }  

代码依然很简单;贴上效果图:


嗯,效果出来了,和预想的一样。下面我们改变edittext的高度为100dp,代码不贴了,效果图是这样的,是不是和你们预想的一样?


看到这里,你发现问题没有?有些人说没有。真没有吗?哈哈,确实没有,我刚开始写博客的时候也纳闷,怎么效果和出来的一样啦,(我预想的是edittext的高度是不变的)这让我怎么写?哈哈,不过灵光一现,我就发现问题了,回到代码我来总结一下一个很重要的问题,我们是讲viewgroup root这个参数的,root是什么意思?是根的意思,我们也可以把它理解为父视图,我们以上代码都是把他写为null的,那么问题来了,不是null的时候呢?我们来改代码:把linearlayout高度改为100dp

dialog_item布局:

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/linearLayout"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="100dp"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <EditText  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:background="#C7EDCC"  
  12.         android:hint="请输入密码" >  
  13.     </EditText>  
  14.   
  15. </LinearLayout>  
其他不变,我们发现效果图是这样的:


哎呦我草,你们发现没有,高度没有变化啊,是不是和我有同样一个疑问,难道不是100dp吗?这时候,我们的root参数要起作用了,只需要改一步,代码贴一部分:

[java]  view plain copy
  1. @Override  
  2.         public View getView(int position, View convertView, ViewGroup parent) {  
  3.             // 我们这里方便起见,不考虑优化,直接用了  
  4.   
  5.             if (convertView == null) {  
  6.                 LayoutInflater inflater = LayoutInflater  
  7.                         .from(MainActivity.this);  
  8.                 //注意,这里改了,为什么这么写,你们自行百度,我不写原因了,篇幅已经远远超过了我的预期  
  9.                 convertView = inflater.inflate(R.layout.dialog_item,parent,false);  
  10.             }  
  11.             return convertView;  
  12.         }  
  13.   
  14.     }  
效果就出来了(注意一下颜色,我故意设置的两个不同颜色)


这个时候你发现高度是100dp了,而edittext的高度还是wrapcontent,这也就是为什么我们平时不注意这个参数写的代码也可以运行的原因,因为我们直接修改的是里面控件的高度。有时候写代码不注意这个很容易出错,比如上一篇博客里写的自定义button,你把继承自linearlayout改成继承view,后面参数写成null,一定会报错的,root参数的意义就是把你当前的布局绑定到一个根视图上,然后形成一个view。不知道你们明白没有,我也只是记录一下我的理解,我有理解错误的地方,恳请大神拍砖,我也不分析源码,因为初学者学起来很蛋疼,而且csdn有很多好的博客,我也借鉴了他们的,更重要的是我也是菜鸟,我也分析不了,只能从应用层面先理解一下了。

说到这里,我想到一个问题,怎么把alertdialog里的edittext也变成这种形式呢?你们想想。很简单的,我已经实现了,图贴上来当结束语吧 


题外话:写一篇博客,我一开始觉得很简单,写下来发现很难。就是这么一个简单的知识点,讲的也是云里雾里,我写博客就当是一个学习的记录,我讲的这些一定有很多漏洞,甚至是完全错误的,我只是抛砖引玉,csdn博客上大神太多,你们可以参考他们的讲解,很详细,至于写博客,我特别建议大家抽出时间来写写,因为在写这篇博客的时候发现,inflate方法的第三个参数也很值得研究,不信?你把listview的填充布局第三个参数改为true试试?(亲测,在dialog里填充,第三个参数无所谓,都能实现预想效果),这是为什么呢?再次警告,我讲的不一定正确委屈
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值