LayoutInflater

通过
Activity实例创建启动后界面如何生成

这篇文章我们知道,activity中的绑定的layout中的每一个view能够被创建,
并且 activity context下能够 findViewById 去查询每个对应的view。
都是因为

LayoutInflater.from(mContext).inflate(resId, contentParent);

LayoutInflater指定了当前的上下文也就是actiivity。调用了 inflate方法,传入布局的id号,以及父布局。
这一步具体源码内容是什么可以看下上面那篇文章,
大概就是为指定上下文的指定layout创建 每一个view。

所以说

对于一个没有被载入或者想要动态载入的界面,都需要使用LayoutInflater.inflate()来载入;
或者说如果你想要获取指定一个layout中的每个view 就可以 利用 LayoutInflater.inflate()
操作如下

获得 LayoutInflater 实例的三种方式

LayoutInflater inflater = getLayoutInflater();  //调用Activity的getLayoutInflater()

LayoutInflater localinflater =  (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

LayoutInflater inflater = LayoutInflater.from(context);   

3种操作都一样,这三种方式最终本质是都是调用的Context.getSystemService()。

inflate 方法
有以下几种过载形式,返回值均是 View 对象,如下:

 public View inflate (int resource, ViewGroup root)  

 public View inflate (XmlPullParser parser, ViewGroup root)  
 
 public View inflate (XmlPullParser parser, ViewGroup root, boolean attachToRoot)  
 
 public View inflate (int resource, ViewGroup root, boolean attachToRoot)  

使用如下:

public class Main2Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        LayoutInflater inflater = LayoutInflater.from(this);
        //这里得指定一个root父容器
        ViewGroup root = findViewById(R.id.linearlayout);
        //这样就得到了这个 R.layout.test这个view了
        View view = inflater.inflate(R.layout.test, root);
        //后续就根据findViewById获取R.layout.test中的每个view了
        TextView textView = view.findViewById(R.id.text_view);


    }

}

注意在
自定义viewgroup中使用去inflate 添加view的时候

public class GeometryView extends FrameLayout {
    public static final String TAG = "GeometryView";


    public GeometryView( Context context) {
        super(context);
        initRoation(context);

    }

    public GeometryView( Context context,  AttributeSet attrs) {
        super(context, attrs);
        initRoation(context);
    }

    private void initRoation(Context context) {
        Log.d(TAG, "initRoation: ");
        LayoutInflater inflater = LayoutInflater.from(context);
        View Roation = inflater.inflate(
                R.layout.gemetry_roation, this);
    }


}
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="24dp"
    android:layout_height="24dp"
    android:background="@drawable/ic_rotation"
    android:layout_marginStart="14dp"
    android:layout_marginTop="46dp"
    android:id="@+id/rotation">
</Button>
View Roation = inflater.inflate(
                R.layout.gemetry_roation, this);

如果这个root参数传进去 this的的话,就已经代表把这个view给 add 进viewgroup了,
不需要在

 addView(Roation); 

并且这个button的属性会听从传进去的this 也就是 FrameLayout

而如果你这个root 设置的是null 那么才需要自己再次添加addView

 View Roation = inflater.inflate(
                R.layout.gemetry_roation, null);
        addView(Roation);

并且这个button的属性不会听从 FrameLayout

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值