关于LayoutInflater的一些自我理解

时间:2023-01-29 16:20:16

刚开始接触安卓开发,感觉有些东西还是要加入点自己的理解的好。如果能帮上观者当然是最好的,但由于没有一个成套的理论在,所以可能有不标准的地方,还希望各位dalao指证

 

有关LayoutInflater部分,之前创建的所有GUI组建都是现成的组件,因此直接可以通过findViewById来寻找对应的组件。但是秉承不重复造*的想法以及组件不够用难免需要自己造*的情况,有时候需要自己创建一个组合的组件,然后这个组件进行批量使用。这时候就需要先创建一个新的XML,然后在里面添加相关组件,将这个活动作为一个打包成的组件批量使用。首先看组件部分的代码:假设我们需要创建一个IOS样式的Title组件,部分代码如下:

<?xml version="1.0" encoding="utf-8"?>
    <!--图片部分来自<第一行代码,可在作者博客中找到>-->
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/title_bg">
  <!--整体布局为LinearLayout,并设定背景为特定图片-->
  <!--按照顺序依次插入GUI组件,并设定相关属性-->
<Button android:id="@+id/title_back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="5dp" android:text="back" android:textColor="#fff" android:background="@drawable/back_bg"/> <TextView android:id="@+id/title_text" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_weight="1" android:gravity="center" android:text="Title Text" android:textColor="#fff" android:textSize="24sp"/> <Button android:id="@+id/title_edit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="5dp" android:background="@drawable/edit_bg" android:text="Edit" android:textColor="#fff"/> <!--margin用于设定左右方向上对齐的距离--> </LinearLayout>

代码大致效果如下:

关于LayoutInflater的一些自我理解

此时我们创建了一个布局用XML文件,但是并没有对这堆组件进行操作堆对应文件(或者Activity,此处因为不是直接创建一个Activity,所以还有待考证)。此时需要创建一个对应的文件用于操作对应的组合组件(也就是右边那个奇丑无比的title部分)

主要代码如下:

public class TitleLayout extends LinearLayout {

    public TitleLayout(Context context, AttributeSet attrs){
        super(context,attrs);
        LayoutInflater.from(context).inflate(R.layout.title,this);
    //this 为当前活动
        //LayoutInflater是找整个的布局文件,而findView则是针对单个组建进行处理
        //调用这个title的activity本身就是一个活动,因此是将调用的活动作为父活动包含title这个活动
        Button titleBack = (Button)findViewById(R.id.title_back);
        Button titleEdit = (Button)findViewById(R.id.title_edit);
        titleBack.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                ((Activity)getContext()).finish(); //获取当前的活动并进行完成处理
            }
        });

        titleEdit.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                //跳出toast
                Toast.makeText(getContext(), "You clicked Edit button", Toast.LENGTH_SHORT).show();
            }
        });
    }

}

其中个人不是很理解的一句在于:

LayoutInflater.from(context).inflate(R.layout.title,this);

对于这部分的个人解释,我是这样理解的:

首先整个句子只有后半部分的title和this为自定义部分,前面全部套用公式(公式部分自行百度),因此首先对后面的部分进行解释。

title为对应调用的布局文件。与findViewById不同,layoutinflater调用的是整个xml布局文件。而后面的this,则是调用这个布局(或者组组件)的布局,类似父布局,而后面的getContext()等方法的直接调用则都是针对调用这个子布局的父布局而言的。因此。每当有布局调用了这个组合布局,则会自动调用以下的按钮监视器函数,从而大大减少程序的重复写作需求