LayoutParams

layoutParams是view用来描述自己在父View中的一些位置参数信息。






    

        
    





xml布局

这里后去xml中的view,通过getLayoutParams得到的是RelativeLayout.LayoutParams,得知获取的是父view类型的layoutParams。

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        LinearLayout view = findViewById(R.id.view_r);
        ViewGroup.LayoutParams params = view.getLayoutParams();
        if (params instanceof LinearLayout.LayoutParams) {
            Log.i("jinwei", "LinearLayout");
        } else if (params instanceof RelativeLayout.LayoutParams) {
            Log.i("jinwei", "RelativeLayout");
            RelativeLayout.LayoutParams p = (RelativeLayout.LayoutParams) view.getLayoutParams();
            p.setMarginStart(200);
            p.addRule(RelativeLayout.ALIGN_START);
        } else if (params instanceof FrameLayout.LayoutParams) {
            Log.i("jinwei", "FrameLayout");
        } else if (params instanceof ViewGroup.MarginLayoutParams) {
            Log.i("jinwei", "MarginLayoutParams");
        }
    }

ViewGroup.LayoutParams
这是顶级的params,它有很多子类params,基本上只要是布局layout都会有一个继承于ViewGroup.LayoutParams的子类。我们可以通过这些LayoutParams,动态多布局设置位置参数了。

你可能感兴趣的:(LayoutParams)