自定义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 <i>parent</i> 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;
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值