约束布局宽度和Java中的高度为根父

时间:2018-04-12 15:14:43

标签: java android android-constraintlayout

这是我的代码,也许是因为某些原因我无法将TextView和Button放在中间位置。我的代码出了什么问题?

ConstraintSet constraintSet = new ConstraintSet();
        int parentId = ConstraintSet.PARENT_ID;

    //parent
    ConstraintLayout orderProcessedLayout = new ConstraintLayout(getApplicationContext());
    int constraintId = orderProcessedLayout.getId();

    //Text View
    TextView thanksTextView = new TextView(this);
    int textViewId = thanksTextView.getId();
    orderProcessedLayout.addView(thanksTextView, 0);

    //ButtonView
    Button reOrderButton = new Button(this);
    int buttonViewId = reOrderButton.getId();
    orderProcessedLayout.addView(reOrderButton, 1);

    /*Setting Views Attributes*/
    //Layout View
    //setting constraints
    constraintSet.clone(orderProcessedLayout);  //cloning ConstraintSet
    ConstraintLayout.LayoutParams constraintParams = new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.WRAP_CONTENT, ConstraintLayout.LayoutParams.WRAP_CONTENT);
    constraintParams.setMargins(16,16,16,16);
    orderProcessedLayout.setLayoutParams(constraintParams);
    //todo set the width and height to be match constraint

    //Text View
    CharSequence thanksText = "THANK YOU";
    thanksTextView.setText(thanksText);
    thanksTextView.setTextColor(Color.BLUE);
    thanksTextView.setHighlightColor(Color.BLACK);

    //setting constraints
    constraintSet.connect(textViewId, ConstraintSet.START, constraintId, ConstraintSet.START);
    constraintSet.connect(textViewId, ConstraintSet.END, constraintId, ConstraintSet.END);
    constraintSet.connect(textViewId, ConstraintSet.TOP, constraintId, ConstraintSet.TOP);
    constraintSet.connect(textViewId, ConstraintSet.BOTTOM, constraintId, ConstraintSet.BOTTOM);

    //Button View
    CharSequence reOrderText = "ORDER AGAIN";
    reOrderButton.setText(reOrderText);

    //Button Action
    reOrderButton.setClickable(true);
    reOrderButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            orderCancelled(null);
        }
    });

    //setting constraints
    constraintSet.connect(buttonViewId, ConstraintSet.START, constraintId, ConstraintSet.START);
    constraintSet.connect(buttonViewId, ConstraintSet.END, constraintId, ConstraintSet.END);
    constraintSet.connect(buttonViewId, ConstraintSet.BOTTOM, constraintId, ConstraintSet.BOTTOM);
    constraintSet.connect(buttonViewId, ConstraintSet.TOP, textViewId, ConstraintSet.BOTTOM);

    //applying everything
    constraintSet.applyTo(orderProcessedLayout);

当我将宽度和高度设置为MATCH_CONSTRAINT或MATCH_CONSTRAINT_SPREAD时,我的视图就会从屏幕上消失。

我在这里做错了什么?

When width and height are set to WRAP_CONTENT or MATCH_PARENT

您是否有解决方案来更正设计,使其看起来像this

1 个答案:

答案 0 :(得分:1)

您创建的所有视图都将其ID设置为-1。首先,您需要为它们设置新的不同ID。为此,您可以使用View类中的静态方法:View.generateViewId(),如下所示:

TextView thanksTextView = new TextView(this);
thanksTextView.setId(View.generateViewId());
int textViewId = thanksTextView.getId();

其次,不要使用ConstraintLayout.LayoutParams在运行时设置边距,而是建议使用documentation中提到的ConstraintSet

考虑到这些更改,代码应如下所示:

ConstraintSet constraintSet = new ConstraintSet();

//parent
ConstraintLayout orderProcessedLayout = new ConstraintLayout(getApplicationContext());
orderProcessedLayout.setId(ConstraintSet.PARENT_ID);
int constraintId = orderProcessedLayout.getId();

//Text View
TextView thanksTextView = new TextView(this);
thanksTextView.setId(View.generateViewId());
int textViewId = thanksTextView.getId();
orderProcessedLayout.addView(thanksTextView, 0);

//ButtonView
Button reOrderButton = new Button(this);
reOrderButton.setId(View.generateViewId());
int buttonViewId = reOrderButton.getId();
orderProcessedLayout.addView(reOrderButton, 1);

/*Setting Views Attributes*/
//Layout View
//setting constraints
constraintSet.clone(orderProcessedLayout);  //cloning ConstraintSet
constraintSet.setMargin(constraintId, ConstraintSet.START, 16);
constraintSet.setMargin(constraintId, ConstraintSet.END, 16);
constraintSet.setMargin(constraintId, ConstraintSet.TOP, 16);
constraintSet.setMargin(constraintId, ConstraintSet.BOTTOM, 16);

//Text View
CharSequence thanksText = "THANK YOU";
thanksTextView.setText(thanksText);
thanksTextView.setTextColor(Color.BLUE);
thanksTextView.setHighlightColor(Color.BLACK);

//setting constraints
constraintSet.connect(textViewId, ConstraintSet.START, constraintId, ConstraintSet.START);
constraintSet.connect(textViewId, ConstraintSet.END, constraintId, ConstraintSet.END);
constraintSet.connect(textViewId, ConstraintSet.TOP, constraintId, ConstraintSet.TOP);
constraintSet.connect(textViewId, ConstraintSet.BOTTOM, constraintId, ConstraintSet.BOTTOM);

//Button View
CharSequence reOrderText = "ORDER AGAIN";
reOrderButton.setText(reOrderText);

//Button Action
reOrderButton.setClickable(true);
reOrderButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        orderCancelled(null);
    }
});

//setting constraints
constraintSet.connect(buttonViewId, ConstraintSet.START, textViewId, ConstraintSet.START);
constraintSet.connect(buttonViewId, ConstraintSet.TOP, textViewId, ConstraintSet.BOTTOM);

//applying everything
constraintSet.applyTo(orderProcessedLayout);