我想创建一个日历日视图,就像谷歌一样。
为此,我想动态地创建时间线。我现在就是这么做的:
TextView nextTime = new TextView(this);
View nextView = new View(this);
View nextLine = new View(this);
ConstraintLayout.LayoutParams lineParams = new
ConstraintLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT);
ConstraintLayout.LayoutParams timeParams = new
ConstraintLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT);
ConstraintLayout.LayoutParams viewParams = new
ConstraintLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT);
viewParams.topToBottom = firstView.getId();
viewParams.leftToLeft = firstView.getId();
viewParams.height = Utils.changeDPItoPX(26, displayMetrics);
viewParams.width = Utils.changeDPItoPX(0, displayMetrics);
viewParams.setMargins(0, Utils.changeDPItoPX(16, displayMetrics),0,0);
nextView.setLayoutParams(viewParams);
constraintLayout.addView(nextView);
timeParams.topToTop = nextView.getId();
timeParams.bottomToBottom = nextView.getId();
timeParams.leftToLeft = nextView.getId();
timeParams.width = ViewGroup.LayoutParams.WRAP_CONTENT;
timeParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
timeParams.setMargins(Utils.changeDPItoPX(16, displayMetrics), 0,0,0);
nextTime.setLayoutParams(timeParams);
constraintLayout.addView(nextTime);
lineParams.height = Utils.changeDPItoPX(2, displayMetrics);
lineParams.bottomToBottom = nextTime.getId();
lineParams.endToEnd = nextView.getId();
lineParams.startToEnd = nextTime.getId();
lineParams.topToTop = nextTime.getId();
lineParams.setMargins(Utils.changeDPItoPX(8, displayMetrics), 0,
Utils.changeDPItoPX(8, displayMetrics), 0);
lineParams.width = Utils.changeDPItoPX(0, displayMetrics);
nextLine.setLayoutParams(lineParams);
constraintLayout.addView(nextLine);
在活动的布局xml中,我已经有了第一个时间戳。所以这个代码应该添加下一个。看起来是这样的:
| 00:00——|
| 01:--0--0---------|
时间线已经超过我的时间了,它也会到达屏幕的末尾。
但它应该看起来像上面的时间戳。
我做错什么了?为什么它忽略了约束?
最佳答案
使用ConstraintSet而不是布局参数将视图添加到ConstraintLayout。
您可以参考以下链接以了解如何使用constraintset
https://developer.android.com/reference/android/support/constraint/ConstraintSet.html