gpt4 book ai didi

Android - 对新添加的 ImageView 和 TextView 设置约束

转载 作者:行者123 更新时间:2023-11-29 01:06:12 25 4
gpt4 key购买 nike

我有一个约束布局 View ,我在其中以编程方式添加 circleimageviews 和 TextView ,但是,当我这样做时,UI 根本不受影响,我找不到错误,如果有人能帮助我,我将不胜感激问题。

我使用一个函数将所有 ImageView 和 TextView 添加到我的布局中,之后我调用另一个函数来设置我需要的约束。

关于我的 UI 设计,我想将 ImageView 显示为一个由 3 列组成的表格,行在运行时确定,我通过将它们彼此相邻并在需要时彼此下方来实现约束集。每个 ImageView 上方是 TextView 中的名称

这是我的代码:

private void initializeFriendsInImageViews(){
friendsImageViews=new ArrayList<ImageView>();
friendNamesTextViews=new ArrayList<TextView>();

//Initializing Layout params
ConstraintLayout.LayoutParams textViewLayoutParams = new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
ConstraintLayout.LayoutParams circleImageViewLayoutParams = new ConstraintLayout.LayoutParams(220, 220);

//Declaring both views
CircleImageView friendPic;
TextView friendName;

for(int i=0; i<countFriends; i++) {
//Initializing both views
friendPic = new CircleImageView(getActivity());
friendName = new TextView(getActivity());

//Setting IDs
friendPic.setId(i);
friendName.setId(countFriends+i);

//Setting layout params
friendPic.setLayoutParams(circleImageViewLayoutParams);
friendName.setLayoutParams(textViewLayoutParams);

//Setting border
friendPic.setBorderColor(ContextCompat.getColor(getActivity(), R.color.border_grey));
friendPic.setBorderWidth(8);

//Load friend picture into imageview
Picasso.with(getActivity()).load(Uri.parse(friendsPicURLs.get(i))).transform(new CircleTransform()).into(friendPic);

//Load friend name into text view
friendName.setText(friendNames.get(i));

//add image view to list of image views
friendsImageViews.add(friendPic);
//add text view to list of text views
friendNamesTextViews.add(friendName);

//Add image view to my layout
friendsConstraintLayout.addView(friendPic);
//Add text view to my layout
friendsConstraintLayout.addView(friendName);
}
addConstraints();
}

private void addConstraints(){
//Initializing constraint set
ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(friendsConstraintLayout);
for (int i=0;i<countFriends;i++){
//positioning a new row of friends (circle image views)
if (i==0){
constraintSet.connect(friendsImageViews.get(i).getId(),ConstraintSet.LEFT,friendsConstraintLayout.getId(),ConstraintSet.LEFT,15);
constraintSet.connect(friendsImageViews.get(i).getId(),ConstraintSet.TOP,friendsConstraintLayout.getId(),ConstraintSet.TOP,15);
}
else if ((i+1)<=3){
constraintSet.connect(friendsImageViews.get(i).getId(),ConstraintSet.LEFT,friendsImageViews.get(i-1).getId(),ConstraintSet.RIGHT);
constraintSet.connect(friendsImageViews.get(i).getId(),ConstraintSet.TOP,friendsImageViews.get(i-1).getId(),ConstraintSet.TOP);
constraintSet.connect(friendsImageViews.get(i).getId(),ConstraintSet.BOTTOM,friendsImageViews.get(i-1).getId(),ConstraintSet.BOTTOM);
}
else if ((i+1)>3){
constraintSet.connect(friendsImageViews.get(i).getId(),ConstraintSet.LEFT,friendsImageViews.get(i-3).getId(),ConstraintSet.LEFT);
constraintSet.connect(friendsImageViews.get(i).getId(),ConstraintSet.RIGHT,friendsImageViews.get(i-3).getId(),ConstraintSet.RIGHT);
constraintSet.connect(friendsImageViews.get(i).getId(),ConstraintSet.TOP,friendsImageViews.get(i-3).getId(),ConstraintSet.BOTTOM);
}

//positioning friend names
constraintSet.connect(friendNamesTextViews.get(i).getId(),ConstraintSet.LEFT,friendsImageViews.get(i).getId(),ConstraintSet.LEFT);
constraintSet.connect(friendNamesTextViews.get(i).getId(),ConstraintSet.RIGHT,friendsImageViews.get(i).getId(),ConstraintSet.RIGHT);
constraintSet.connect(friendNamesTextViews.get(i).getId(),ConstraintSet.BOTTOM,friendsImageViews.get(i).getId(),ConstraintSet.TOP);
}
//Apply Constraints
constraintSet.applyTo(friendsConstraintLayout);
}

最佳答案

每次添加 View 时都需要创建一组新的布局参数。每个连续添加的 View 都在破坏先前添加的 View 的布局参数。执行以下操作:

//Setting layout params
circleImageViewLayoutParams = new ConstraintLayout.LayoutParams(220, 220);
friendPic.setLayoutParams(circleImageViewLayoutParams);
textViewLayoutParams = new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.WRAP_CONTENT, ConstraintLayout.LayoutParams.WRAP_CONTENT);
friendName.setLayoutParams(textViewLayoutParams);

虽然这现在可能不会给您带来问题,但将来可能会造成问题:我会避免使用零作为 id。

关于Android - 对新添加的 ImageView 和 TextView 设置约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46983854/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com