[英]Can not add view programmatically in a constraint layout
我正在尝试在Android应用程序中使用Java代码添加视图,但是即使是基本测试,Textview也不会显示
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ConstraintLayout layout = findViewById(R.id.form1);
TextView mazzo = new TextView(this);
mazzo.setId(View.generateViewId());
mazzo.setText(m_Text);
mazzo.setLayoutParams(new ConstraintLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
layout.addView(mazzo);
ConstraintSet set = new ConstraintSet();
set.clone(layout);
set.connect(mazzo.getId(), ConstraintSet.TOP, layout.getId(), ConstraintSet.TOP, 18);
set.connect(mazzo.getId(), ConstraintSet.LEFT, layout.getId(), ConstraintSet.LEFT, 18);
set.applyTo(layout);
弄清楚了,我有另一个约束布局作为我的主要布局,显然如果您在内部放置另一个约束布局,则必须连接每一侧,否则在连接右侧和底部后也不会显示出来,因此它也可以工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.