RelativeLayout add rule “RelativeLayout.LEFT_OF” not working

前端 未结 6 1304
小蘑菇
小蘑菇 2020-12-17 10:50

I have a relativeLayout like below:



        
6条回答
  •  [愿得一人]
    2020-12-17 11:40

    To perform it, use predefined view ID or declare one. In values folder create ids.xml then add a Item like this:

    
    

    use this id in your code where you are creating new Instance of view like this:

    RelativeLayout layout=new RelativeLayout(context);
    
    ImageView imageView = new ImageView(context);
    imageView.setId(R.id.imageViewID);
    
    RelativeLayout.LayoutParams layoutParams = new LayoutParams(50, 50);
    layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
    layout.addView(imageView, layoutParams);
    
    TextView  textView = new TextView(context);
    
    RelativeLayout.LayoutParams textViewParams= new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    textViewParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
    textViewParams.addRule(RelativeLayout.BELOW, imageView.getId());
    layout.addView(nameView, nameLayoutParams);
    

    or we can directly use this function View.generateViewId() to perform the same. Like this:

    imageView.setId(View.generateViewId());
    

提交回复
热议问题