LayoutInflater和LayoutParams

1,275 阅读3分钟

LayoutInflater

作用:实例化一个布局xml文件到相应的View视图中,相当于一个视图填充器。

使用方法:1.获取LayoutInflater对象;

                 2.调用LayoutInflater对象的inflater方法进行填充。

细节

1.获取LayoutInflater的三种方式

LayoutInflater inflater = LayoutInflater.from(context);

LayoutInflater inflater = getLayoutInflater();

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

通过源码可以发现前两种实际都是通过第三种方式实现的。

2.将.xml布局转化为View的方式

       1.使用View的inflate方法,不用获取LayoutInflater即可获取View;

       2.使用LayoutInflater.inflate();

有四种调用方式:
mLayoutInflater.inflate(@LayoutRes int resource,@Nullable ViewGroup root);
mLayoutInflater.inflate(XmlPullParser parser,@Nullable ViewGroup root);
mLayoutInflater.inflate(@Layoutres int resourse,@Nullable ViewGroup root ,boolean attachToRoot);
mLayoutInflater.inflater(XmlPullParser parser,@Nullable ViewGroup root,boolean attachToToot) ;

常用的主要是第一和第三种方式,根据源码,最终调用的都是第四种方式。

方法中的参数及其意义:

@LayoutRes int resource : 是需要传入的布局的资源ID;
@Nullable ViewGroup root : 需要附加到resource资源文件的父控件,即调用inflate方法会得到一个View对象,root参数就是接受该对象的容器;
boolean attachToRoot : 是否把inflate得到的View对象添加到root中,该参数为false时,表示不直接添加到root,true时,表示直接添加到root中;

3.具体使用例子

1.通过inflate方式3,参数3传入true:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        LinearLayout ll_root = (LinearLayout)findViewById(R.id.ll_root) ;      //Activity的根布局
        getLayoutInflater().inflate(R.layout.inflatelayout,ll_root,true);            //进行布局填充
    }

2.通过inflate方式3,参数3传入false:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        LinearLayout ll_root = (LinearLayout)findViewById(R.id.ll_root) ;
        View view = getLayoutInflater().inflate(R.layout.inflatelayout,ll_root,false);
        ll_root.addView(view);
    }

       在此方式中设置root不为空,是为了让inflatelayout.xml的根布局宽高有效,attachToRoot就是不自动将inflateLayout.xml填充到父容器中。

3.通过infalte方式1:即例子1可简化为如下

mLayoutInflater.inflate(@LayoutRes int resource,@Nullable ViewGroup root);
当root不为null时,直接把View填充到root中
当root为null时,不自动将View填充到root中
即例子1可简化为如下:
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        LinearLayout ll_root = (LinearLayout)findViewById(R.id.ll_root) ;      //Activity的根布局
        getLayoutInflater().inflate(R.layout.inflatelayout,ll_root);            //进行布局填充
    }

引用自www.jianshu.com/p/36b200a0b…

LayoutParams

作用:动态控制子View的位置,常用来设置宽高。

使用方法:1.获取布局或者控件对象;

                 2.通过父布局的LayoutParams构造方法设置宽高并返回实例;

                 3.通过布局的setlayoutparams方法传入步骤2实例;

                 4.通过父布局的addView方法传入布局。

细节:

1.具体使用例子:

public class LayoutParamsFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        LinearLayout ll = new LinearLayout(getContext());
        // ll的父容器是MainActivity中的FrameLayout
        ll.setLayoutParams(new FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        ll.setGravity(Gravity.CENTER);
        ll.setBackgroundResource(android.R.color.holo_blue_bright);

        TextView tv = new TextView(getContext());
        // tv的父容器是LinearLayout
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(160, 160);
        tv.setLayoutParams(layoutParams);// ①
        tv.setBackgroundResource(android.R.color.holo_red_dark);
        tv.setText(getText(R.string.tv));

        ll.addView(tv);// ②
        return ll;
    }
}

上面 ①、② 两行代码可以简化为一行,替换为 addView(View child, LayoutParams params) 这个重载方法,在添加到父布局时,设置 LayoutParams,通知父布局如何摆放自己。

ll.addView(vt, layoutParams);// 在添加到父布局的时候

getLayoutParams

作用:在不使用代码动态布局的情况下,大都是先通过 getLayoutParams() 获取 LayoutParams ,然后进行赋值,最后通过 setLayoutParams()设回控件。

细节

public class LayoutParamsFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        LinearLayout ll = new LinearLayout(getContext());
        // ll的父容器是MainActivity中的FrameLayout
        ll.setLayoutParams(new FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        ll.setGravity(Gravity.CENTER);// 子控件居中
        ll.setBackgroundResource(android.R.color.holo_blue_bright);

        TextView tv = new TextView(getContext());
        ll.addView(tv);// 添加到父控件,此时会构造一个LayoutParams出来。

        LinearLayout.LayoutParams ll_params = (LinearLayout.LayoutParams) tv.getLayoutParams();
        ll_params.width = 160;
        ll_params.height = 160;
        tv.setLayoutParams(ll_params);
        tv.setBackgroundResource(android.R.color.holo_red_dark);
        tv.setText(getText(R.string.tv));

        return ll;
    }
}

引用自:www.jianshu.com/p/36b200a0b…