2021年4月5日星期一

Can't see constraint layout added to linear layout

I created a constraint layout, set its layout params to (Match_Parent, Wrap_Content) and then added it to an existing linear layout programmatically - all without exceptions. I ran the application and could not see the added constraint layout on the screen.

Anyone know why?

here is the code:

private void addGroups(){          int i = 0;          while(true){              JSONObject group = spm.getObject("grp" + i);                ConstraintLayout thumbnail = new ConstraintLayout(this);              thumbnail.setId(View.generateViewId());                try {                  thumbnail.setLayoutParams(new ViewGroup.LayoutParams(                          ViewGroup.LayoutParams.MATCH_PARENT,                          ViewGroup.LayoutParams.WRAP_CONTENT));                  thumbnail.setClickable(true);                    ConstraintSet set = new ConstraintSet();                  set.clone(thumbnail);                    ImageView groupImage = new ImageView(this);                  groupImage.setId(View.generateViewId());                  thumbnail.addView(groupImage, 0);                  groupImage.setLayoutParams(new ConstraintLayout.LayoutParams(50, 50));                  set.connect(groupImage.getId(), ConstraintSet.TOP, thumbnail.getId(), ConstraintSet.TOP, 8);                  set.connect(groupImage.getId(), ConstraintSet.RIGHT, thumbnail.getId(), ConstraintSet.RIGHT, 8);                  set.connect(groupImage.getId(), ConstraintSet.BOTTOM, thumbnail.getId(), ConstraintSet.BOTTOM, 8);                  set.connect(groupImage.getId(), ConstraintSet.BOTTOM, thumbnail.getId(), ConstraintSet.BOTTOM, 0);                  set.setHorizontalBias(groupImage.getId(), 0);                  groupImage.setImageResource(android.R.drawable.alert_light_frame);                  Bitmap img;                  try{                      img = spm.StringToBitMap(group.getString("image"));                  }catch(Exception e){                      img = null;                  }                  if(img != null) groupImage.setImageBitmap(img);                    TextView groupName = new TextView(this);                  groupName.setId(View.generateViewId());                  thumbnail.addView(groupName, 1);                  groupName.setLayoutParams(new ConstraintLayout.LayoutParams(0, 42));                  set.connect(groupName.getId(), ConstraintSet.TOP, thumbnail.getId(), ConstraintSet.TOP, 8);                  set.connect(groupName.getId(), ConstraintSet.RIGHT, groupImage.getId(), ConstraintSet.LEFT, 8);                  set.connect(groupName.getId(), ConstraintSet.BOTTOM, thumbnail.getId(), ConstraintSet.BOTTOM, 8);                  set.connect(groupName.getId(), ConstraintSet.BOTTOM, thumbnail.getId(), ConstraintSet.BOTTOM, 8);                  groupName.setText(group.getString("name"));                    set.applyTo(thumbnail);                    System.out.println("Successfully created thumbnail " + i);                }catch(Exception e){                  System.out.println("Error1 " + e + " ================================================");                  i++;                  continue;              }                TNCon.addView(thumbnail, i);              System.out.println(thumbnail.toString());              i++;          }      }  

(spm is a class-managing the SharedPreferances and getObject() returns a JSON, saved in the SP as a string in JSON form - all the JSON-related things work. The program creates the constraint layout element based on the JSON I saved.)

https://stackoverflow.com/questions/66948893/cant-see-constraint-layout-added-to-linear-layout April 05, 2021 at 02:08PM

没有评论:

发表评论