以编程方式构建约束布局

时间:2017-10-26 15:27:39

标签: java android xml android-layout android-constraintlayout

我正在尝试以编程方式构建约束布局,并且我必须能够随意重新排列布局的各个部分。我将所有布局放入约束布局并创建指南,以便每个视图只占一定的百分比。我已经在下面发布了约束布局的代码,但基本上我添加了一个视图后,我创建并添加了随之而来的指南。之后,我为“连接”提供了参数。 ConstraintSet中的方法在列表中,因为我理解在执行此操作之前需要添加所有视图。添加完所有视图后,我从ConstraintLayout克隆ConstraintSet,然后遍历连接所有视图的连接参数列表......但这样做起作用。

//mConstraintSets holds all info needed to connect the views later

protected Guideline createGuideline(){
        Guideline guideline = new Guideline(mContext);
        mConstraintSets.add(new ManifestConstraintSet(guideline.getId(), true));
        manifestLayout.addView(guideline);
        ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) guideline.getLayoutParams();
        params.guidePercent = layoutPercent; // 45% - range: 0 <-> 1
        guideline.setLayoutParams(params);
        return guideline;
    }

//This will create the constaint for the toolbar
protected void constraintToolbar(int viewID){
    aboveViewID = viewID;

    mConstraintSets.add(new ManifestConstraintSet(viewID, ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START, 0));
    mConstraintSets.add(new ManifestConstraintSet(viewID, ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END, 0));
    mConstraintSets.add(new ManifestConstraintSet(viewID, ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP, 0));

}

//This will create the constraint for a view
protected void constraintView(int viewID, float percent){
    layoutPercent += percent;
    Guideline guideline = new Guideline(mContext);
    Boolean isBottomView = false;

    //Check if the view is the bottom view
    //if so don't create a guideline
    if(layoutPercent < 1f){
        guideline = createGuideline();
    }else{
        isBottomView = true;
    }

    //region left, right, & top constraints are the same for every view
    mConstraintSets.add(new ManifestConstraintSet(viewID, ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START, 0));
    mConstraintSets.add(new ManifestConstraintSet(viewID, ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END, 0));
    mConstraintSets.add(new ManifestConstraintSet(viewID, ConstraintSet.TOP, aboveViewID, ConstraintSet.BOTTOM, 0));
    aboveViewID = viewID;
    //endregion

    if(isBottomView){
        mConstraintSets.add(new ManifestConstraintSet(viewID, ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM, 0));

    }else{
        mConstraintSets.add(new ManifestConstraintSet(viewID, ConstraintSet.BOTTOM, guideline.getId(), ConstraintSet.TOP, 0));
    }
}

protected void addConstraintsToSet(){
    manifestConstraintSet.clone(manifestLayout);

    for (ManifestConstraintSet set:mConstraintSets) {
        if(set.isGuideline){
            manifestConstraintSet.create(set.startView, ConstraintSet.VERTICAL_GUIDELINE);
        }else{
            manifestConstraintSet.connect(set.startView, set.startPos, set.endView, set.endPos, set.margin);
            manifestConstraintSet.constrainWidth(set.startView, ConstraintSet.MATCH_CONSTRAINT);
            manifestConstraintSet.constrainHeight(set.startView, ConstraintSet.MATCH_CONSTRAINT);
        }

    }
    manifestConstraintSet.applyTo(manifestLayout);
}

我不认为我完全理解ConstraintLayouts如何以编程方式真正起作用。如果重要,当我创建视图时,我使用静态生成视图id方法来设置它们的ID。

1 个答案:

答案 0 :(得分:1)

是否像ConstraintLayout中的普通观点一样添加了指南?请查看createsetGuidelineBegin以及其他方法,以设置指南的位置。我没有在您的代码中看到任何指南位置。

我会从一个非常简单的布局开始,只有一个TextView(例如)和一个指南,以确保我理解机制。然后我会扩展到完整的布局。