Android 在ConstraintLayout中为多个ConstraintLayout充气
我正在开发我的第一个android应用程序,遇到了一些麻烦。 在“我的活动”中有以下布局:ScrollView(ConstraintLayout)。 我做了一个单独的布局(一个模板,一个约束列表),我想在我的滚动列表中膨胀它。 我的目的是根据我的模板创建一个列表,我将对其进行充气。 我希望得到如下结果:ScrollView(ConstrintLayout(ConstraintLayout,ConstraintLayout,…)Android 在ConstraintLayout中为多个ConstraintLayout充气,android,scrollview,android-constraintlayout,android-inflate,Android,Scrollview,Android Constraintlayout,Android Inflate,我正在开发我的第一个android应用程序,遇到了一些麻烦。 在“我的活动”中有以下布局:ScrollView(ConstraintLayout)。 我做了一个单独的布局(一个模板,一个约束列表),我想在我的滚动列表中膨胀它。 我的目的是根据我的模板创建一个列表,我将对其进行充气。 我希望得到如下结果:ScrollView(ConstrintLayout(ConstraintLayout,ConstraintLayout,…) ConstraintLayout scrollConstraint=
ConstraintLayout scrollConstraint=
(ConstraintLayout)findviewbyd(R.id.scroll\u约束);
例如(int i=0;听起来您最好在ScrollView中使用LinearLayout(线性布局)(因为听起来您只是希望孩子们显示在彼此下方).你不想使用LinearLayout有什么原因吗?这就是ConstraintLayout和LinearLayout之间的巨大区别。非常感谢,先生。我的问题已经解决了。我不认为LinearLayout是一个真正的解决方案。如果你想拥有可滚动的多个相同视图,RecyclerView应该是你的选择。线性布局之所以有效,是因为必须添加约束才能使它们彼此低于对方。
ConstraintLayout scrollConstraint =
(ConstraintLayout)findViewById(R.id.scroll_constraint);
for(int i=0;i<2;i++) {
LayoutInflater inflater = getLayoutInflater();
View newRoute = inflater.inflate(R.layout.scroll, scrollConstraint, false);
TextView newRouteFrom = (TextView) newRoute.findViewById(R.id.from1);
if(i==1)
newRouteFrom.setText("New route");
scrollConstraint.addView(newRoute);
}