自定义View添加到Viewgroup中layoutparams为空的记录

layoutparams 是子View告诉父View(Viewgoup) 自已在父container 里如何摆放的。看下面代码出现的问题:

public class MainActivity extends Activity {

    private LinearLayout root_ll;

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

        root_ll = (LinearLayout) findViewById(R.id.root_ll); // 根Viewgroup
        // 获取屏幕的宽和高

        Display display = getWindowManager().getDefaultDisplay();
        int width = display.getWidth();
        int height = display.getHeight();

        MyView view = new MyView(getApplicationContext());
        // view.setMinimumHeight(height/2);
        // view.setMinimumWidth(width/2);


        // view 只有被添加到  viewgroup里 才能获取  LayouParmas
                LayoutParams params = view.getLayoutParams();
                params.height = height / 2;
                params.width = width / 2;
                view.setLayoutParams(params);

        view.invalidate(); // 通知view组件重绘

        root_ll.addView(view);


    }

}

报错信息:
这里写图片描述
只怕以会出现这样的问题,是因为 这段代码的顺序

// view 只有被添加到  viewgroup里 才能获取  LayouParmas
                LayoutParams params = view.getLayoutParams();
                params.height = height / 2;
                params.width = width / 2;
                view.setLayoutParams(params);

        view.invalidate(); // 通知view组件重绘

        root_ll.addView(view);

我们把 MyView 创建出来 就开始获取 LayoutParmas
这个时候获取 LayoutParams 其实是一点意义也没有,因为 MyView 还没有成为 root_ll 的儿子呢 也就是说它们现在一点关系也没有,既然没有关系又如何 谈摆放呢!
我们 应该在 MyView 被添加到 父container(root_ll)之后 再获取 layoutparams

MyView view = new MyView(getApplicationContext());
        // view.setMinimumHeight(height/2);
        // view.setMinimumWidth(width/2);


        // view 只有被添加到  viewgroup里 才能获取  LayouParmas

        view.invalidate(); // 通知view组件重绘

        root_ll.addView(view);

        LayoutParams params = view.getLayoutParams();
        params.height = height / 2;
        params.width = width / 2;
        view.setLayoutParams(params);

View 源码:

    /**
     * Get the LayoutParams associated with this view. All views should have
     * layout parameters. These supply parameters to the parent of this
     * view specifying how it should be arranged. There are many subclasses of
     * ViewGroup.LayoutParams, and these correspond to the different subclasses
     * of ViewGroup that are responsible for arranging their children.
     *
     * This method may return null if this View is not attached to a parent
     * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
     * was not invoked successfully. When a View is attached to a parent
     * ViewGroup, this method must not return null.
     *
     * @return The LayoutParams associated with this view, or null if no
     *         parameters have been set yet
     */
    @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
    public ViewGroup.LayoutParams getLayoutParams() {
        return mLayoutParams;
    }

你可能感兴趣的:(Android)