[英]Constraint Layout Width and Height in Java as Root Parent
Here is my code and maybe because of some reason I cannot position my TextView and Button in the middle. 这是我的代码,可能由于某种原因我无法将TextView和Button放在中间。 What is wrong with my code?
我的代码有什么问题?
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);
And when I set width and height to MATCH_CONSTRAINT or MATCH_CONSTRAINT_SPREAD, my views just disappear from Screen. 当我将宽度和高度设置为MATCH_CONSTRAINT或MATCH_CONSTRAINT_SPREAD时,我的视图只会从屏幕上消失。
What have I done wrong here? 我在这里做错了什么?
Do you have a solution to correct the desgin so it looks like 您有解决方案来纠正设计问题吗
All the views that you create have their IDs set to -1. 您创建的所有视图的ID均设置为-1。 So first of all, you need to set new distinct IDs for them.
因此,首先,您需要为它们设置新的不同ID。 To achieve that you can use a static method from the
View
class: View.generateViewId()
like so: 为此,您可以使用
View
类的静态方法: View.generateViewId()
如下所示:
TextView thanksTextView = new TextView(this);
thanksTextView.setId(View.generateViewId());
int textViewId = thanksTextView.getId();
Secondly, instead of using ConstraintLayout.LayoutParams
to set the margin at runtime it is recommended to use ConstraintSet
as mentioned in the documentation . 其次,建议不要使用
ConstraintLayout.LayoutParams
在运行时设置边距,而是建议使用ConstraintSet
如文档中所述。
With these changes in mind the code should look like this: 考虑到这些更改,代码应如下所示:
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.