android 动态addview注意

在Android开发中,动态添加View时,通常会遇到设置LayoutParams的问题。原本通过inflate布局并手动设置LayoutParams却发现效果不理想。经过尝试,发现直接将inflate的布局添加到目标父视图,如mModeListView,可以避免手动设置LayoutParams,从而解决参数未生效的困扰。
摘要由CSDN通过智能技术生成

以前用的这种方式:

inflate布局

        filterLayout = (FrameLayout) inflater.inflate(R.layout.camera_photo_filter, null);

然后再:

创建params

        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, activity
                .getResources().getDimensionPixelSize(R.dimen.bottom_bar_height_optimal));
       params.gravity = Gravity.BOTTOM;

添加view:

        mModeListView.addView(filterLayout,params);

然后觉得很奇怪,为啥我在xml中写了 paramas,还要 new params 呢?


于是抱着试试看的态度

        filterLayout = (FrameLayout) inflater.inflate(R.layout.camera_photo_filter, null) 

        mModeListView.addView(filterLayout);

发现params没有生效




最后解决方法:

   filterLayout = (FrameLayout) inflater.inflate(R.layout.camera_photo_filter, mModeListView);

一句话就好了

害我搞半天



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值