View实例化流程(高级)

本文详细探讨了Android中View的实例化流程,从布局文件解析到View的构造,再到测量、布局和绘制的步骤,揭示了UI组件从无到有的生命周期秘密。
摘要由CSDN通过智能技术生成
View 实例化 流程(高级)
小智
原理:View通过LayoutInflater实现加载

我们一般讲的先把View实例化成对象通过3种方式去实现:
1.LayoutInflater layoutInflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  layoutInflater.inflate(resourceId, root);
2.LayoutInflater layoutInflater=LayoutInflater.from(context);
  layoutInflater.inflate(resourceId, root);
3.View view=View.inflater(context,resourceId, root);
这三种方式的作用都是一样的,第二种其实是对第一种的封装,第三种是对第二种的封装。
使用:
我们一般都是使用这种方式去拿到一个布局,然后添加到另一个布局上面,步骤也很简单。我们来看一下以下代码。
activity_main.xml:
   
   
   
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:id="@+id/main_layout"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent" >
  5. </LinearLayout>
view.xml
   
   
   
  1.        
           
           
    1. <Button xmlns:android="http://schemas.android.com/apk/res/android"
    2. android:layout_width="wrap_content"
    3. android:layout_height="wrap_content"
    4. android:text="Button" >
    5. </Button>


MainActivity java代码
   
   
   
  1.        
           
           
    1. public class MainActivity extends Activity {
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值