【问题标题】:Set ConstraintLayout width to match parent width programmatically以编程方式设置 ConstraintLayout 宽度以匹配父宽度
【发布时间】:2017-05-29 19:13:01
【问题描述】:

在一个 android 应用程序中,我试图以编程方式将自定义的 ConstraintLayout 视图添加到垂直方向的 LinearLayout

我在ConstraintLayouts 中将LayoutParams 设置为MATCH_PARENT 的宽度和WRAP_CONTENT 的高度。但是,当我运行该应用程序时,ConstraintView 全部被卷曲并且内容重叠。下面是一些相关的 sn-ps 和我的应用程序的屏幕截图。我该如何解决这个问题?

public class ItemView extends ConstraintLayout {
    LinearLayout linearButtons;
    LinearLayout linearText;
public ItemView(Context context, String name, String price, ArrayList<String> guests, 
   ArrayList<String> checked, int id) {
...
    addView(linearText);
    addView(linearButtons);
    set.clone(this);
    set.connect(linearText.getId(), ConstraintSet.LEFT, this.getId(), 
      ConstraintSet.LEFT, 8);
    set.connect(linearText.getId(), ConstraintSet.TOP, this.getId(), 
     ConstraintSet.TOP, 8);
    set.connect(linearButtons.getId(), ConstraintSet.RIGHT, this.getId(), 
     ConstraintSet.RIGHT, 8);
    set.connect(linearButtons.getId(), ConstraintSet.TOP, this.getId(), 
     ConstraintSet.TOP, 8);
}

其他地方:

for (Item it:r.getItems()) {
        ItemView itemView = new ItemView(this, it.getName(), nf.format(it.getPrice()), dinerlist, it.getGuests(), i);
        ConstraintLayout.LayoutParams params = new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.MATCH_PARENT);
        itemView.setLayoutParams(params);
        vg.addV[enter image description here][1]iew(itemView);
        Log.d("ItemView Children: ", itemView.getWidth()+" "+itemView.getHeight());

【问题讨论】:

    标签: android android-layout android-constraintlayout


    【解决方案1】:

    在 xml 中的 ConstraintLayout 中,当您想要“MATCH_PARENT”宽度时,您必须将宽度设置为 0dp,然后将 layout_constraintWidth_default 属性设置为“spread”

    以编程方式您也可以这样做:

    a) 设置 0 dp 的宽度和高度

    b) 设置 defaultWidth 和 defaultHeight 约束

        //add the view with 0dp width and height
        val layoutParams = ConstraintLayout.LayoutParams(0, 0)
        val view = View(context)
        view.layoutParams = layoutParams
        view.id = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) View.generateViewId() else R.id.yourLayoutId
        parent.addView(view)
    
        //apply the default width and height constraints in code
        val constraints = ConstraintSet()
        constraints.clone(parent)
        constraints.constrainDefaultHeight(view.id, ConstraintSet.MATCH_CONSTRAINT_SPREAD)
        constraints.constrainDefaultWidth(view.id, ConstraintSet.MATCH_CONSTRAINT_SPREAD)
        constraints.applyTo(parent)
    

    如果你需要 Java:

        //add the view with 0dp width and height
        ConstraintLayout.LayoutParams layoutParams = new ConstraintLayout.LayoutParams(0, 0);
        View view = View(context);
        view.setLayoutParams(layoutParams);
        view.setId(R.id.yourLayoutId);
        parent.addView(view);
    
        //apply the default width and height constraints in code
        ConstraintSet constraints = new ConstraintSet();
        constraints.clone(parent);
        constraints.constrainDefaultHeight(view.getId(), ConstraintSet.MATCH_CONSTRAINT_SPREAD);
        constraints.constrainDefaultWidth(view.getId(), ConstraintSet.MATCH_CONSTRAINT_SPREAD);
        constraints.applyTo(parent);
    

    【讨论】:

    • 抱歉在 kotlin 中回答,您需要 java 吗? -> 我添加了 Java
    • 谢谢你救了我的一天
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-01
    • 2011-03-09
    相关资源
    最近更新 更多