Android 纯代码写布局 RelativeLayout.BELOW 不生效问题解决

eg:

public class MainActivity extends BaseAppCompatActivity {


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        RelativeLayout relativeLayoutRoot = new RelativeLayout(this);
        relativeLayoutRoot.setBackgroundColor(Color.parseColor("#ffffff"));
        RelativeLayout.LayoutParams relativeLayoutRootLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        relativeLayoutRoot.setLayoutParams(relativeLayoutRootLayoutParams);

        LinearLayout linearLayoutNavigation = new LinearLayout(this);
        linearLayoutNavigation.setBackgroundColor(Color.parseColor("#333333"));
        LinearLayout.LayoutParams linearLayoutNavigationLayoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 150);


        linearLayoutNavigation.setLayoutParams(linearLayoutNavigationLayoutParams);

        relativeLayoutRoot.addView(linearLayoutNavigation);

        ((RelativeLayout.LayoutParams)
                linearLayoutNavigation.getLayoutParams()
        ).addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);


        for (int i = 0; i < 5; i++) {

            RelativeLayout relativeLayoutNavigation = new RelativeLayout(this);
            RelativeLayout.LayoutParams relativeLayoutNavigationLayoutParams = new RelativeLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT);
            relativeLayoutNavigation.setLayoutParams(relativeLayoutNavigationLayoutParams);
            linearLayoutNavigation.addView(relativeLayoutNavigation);

            ((LinearLayout.LayoutParams) relativeLayoutNavigation.getLayoutParams()).weight = 1;

            ImageView imageViewIcon = new ImageView(this);
            imageViewIcon.setImageDrawable(getDrawable(R.drawable.tab_bar_message));
            RelativeLayout.LayoutParams imageViewIconLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            imageViewIcon.setLayoutParams(imageViewIconLayoutParams);
            relativeLayoutNavigation.addView(imageViewIcon);



            TextView textViewName = new TextView(this);
            textViewName.setText("消息");
            RelativeLayout.LayoutParams textViewNameLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

            textViewName.setLayoutParams(textViewNameLayoutParams);
            textViewName.setTextColor(Color.parseColor("#ffffff"));
            relativeLayoutNavigation.addView(textViewName);


            ((RelativeLayout.LayoutParams) imageViewIcon.getLayoutParams()).addRule(RelativeLayout.CENTER_IN_PARENT);

            ((RelativeLayout.LayoutParams) textViewName.getLayoutParams()).addRule(RelativeLayout.CENTER_HORIZONTAL);
            Log.e(TAG, "onCreate: "+imageViewIcon.getId() );
            
            // 此处不生效的原因是imageViewIcon.getId() 的值为-1
            ((RelativeLayout.LayoutParams) textViewName.getLayoutParams()).addRule(RelativeLayout.BELOW,imageViewIcon.getId());
            
        }
        setContentView(relativeLayoutRoot);
    }
    private static final String TAG = "MainActivity";
}

原因

不生效的原因是imageViewIcon.getId() 的值为-1

解决方案

给控件设置一个ID

 imageViewIcon.setId(View.generateViewId());
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值