使用ConstraintLayout以编程方式连接设置为任意大小的多个视图 [英] Programmatically connecting multiple views set to any size using ConstraintLayout
问题描述
我正在使用ConstraintLayout beta4,并尝试以编程方式在屏幕上创建水平链.
我遇到的问题是,当我在屏幕上将多个视图以链状连接在一起时
(ex: Left side of layout <-> leftButton <-> rightButton <-> Right side of layout)
那么什么也没出现.
这是无效的代码:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ConstraintLayout layout = (ConstraintLayout) View.inflate(this, R.layout.activity_main, null);
setContentView(layout);
ConstraintSet set = new ConstraintSet();
set.clone(layout);
ConstraintLayout.LayoutParams anySizeParams = new ConstraintLayout.LayoutParams(0,0);
ImageButton leftButton = new ImageButton(this);
leftButton.setId(View.generateViewId());
leftButton.setLayoutParams(anySizeParams);
layout.addView(leftButton);
ImageButton rightButton = new ImageButton(this);
rightButton.setId(View.generateViewId());
rightButton.setLayoutParams(anySizeParams);
layout.addView(rightButton);
set.connect(leftButton.getId(),ConstraintSet.LEFT,layout.getId(),ConstraintSet.LEFT,0);
set.connect(leftButton.getId(),ConstraintSet.RIGHT,rightButton.getId(),ConstraintSet.LEFT,0);
set.connect(leftButton.getId(),ConstraintSet.TOP,layout.getId(),ConstraintSet.TOP,0);
set.connect(leftButton.getId(),ConstraintSet.BOTTOM,layout.getId(),ConstraintSet.BOTTOM,0);
set.connect(rightButton.getId(),ConstraintSet.LEFT,rightButton.getId(),ConstraintSet.RIGHT,0);
set.connect(rightButton.getId(),ConstraintSet.RIGHT,layout.getId(),ConstraintSet.RIGHT,0);
set.connect(rightButton.getId(),ConstraintSet.TOP,layout.getId(),ConstraintSet.TOP,0);
set.connect(rightButton.getId(),ConstraintSet.BOTTOM,layout.getId(),ConstraintSet.BOTTOM,0);
set.applyTo(layout);
}
}
这是上面代码的屏幕截图:以编程方式创建.. >
下面是一些描述相同内容并且可以正常工作的xml,以供参考:
<ImageButton
android:layout_width="0dp"
android:layout_height="0dp"
android:id="@+id/right_button"
app:layout_constraintLeft_toRightOf="@+id/left_button"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
<ImageButton
android:layout_width="0dp"
android:layout_height="0dp"
android:id="@+id/left_button"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/right_button"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
这是xml的屏幕截图:手动创建
您正在做的事情有两个问题.首先,您要为两个对象分配相同的LayoutParams实例-这是行不通的.您需要一个单独的实例:
ConstraintLayout.LayoutParams anySizeParams2 = new ConstraintLayout.LayoutParams(0, 0);
rightButton.setLayoutParams(anySizeParams2);
第二,您的连接有误:
set.connect(rightButton.getId(),ConstraintSet.LEFT,
rightButton.getId(),ConstraintSet.RIGHT,0);
应该是:
set.connect(rightButton.getId(),ConstraintSet.LEFT,
leftButton.getId(),ConstraintSet.RIGHT,0);
否则,它不会创建链.
添加视图后,克隆布局可能会更干净.
I'm using ConstraintLayout beta4 and trying to create a horizontal chain across the screen programmatically.
The problem I'm running into is when I connect multiple views together in a chain across the screen
(ex: Left side of layout <-> leftButton <-> rightButton <-> Right side of layout)
then nothing shows up at all.
Here is the code that does not work:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ConstraintLayout layout = (ConstraintLayout) View.inflate(this, R.layout.activity_main, null);
setContentView(layout);
ConstraintSet set = new ConstraintSet();
set.clone(layout);
ConstraintLayout.LayoutParams anySizeParams = new ConstraintLayout.LayoutParams(0,0);
ImageButton leftButton = new ImageButton(this);
leftButton.setId(View.generateViewId());
leftButton.setLayoutParams(anySizeParams);
layout.addView(leftButton);
ImageButton rightButton = new ImageButton(this);
rightButton.setId(View.generateViewId());
rightButton.setLayoutParams(anySizeParams);
layout.addView(rightButton);
set.connect(leftButton.getId(),ConstraintSet.LEFT,layout.getId(),ConstraintSet.LEFT,0);
set.connect(leftButton.getId(),ConstraintSet.RIGHT,rightButton.getId(),ConstraintSet.LEFT,0);
set.connect(leftButton.getId(),ConstraintSet.TOP,layout.getId(),ConstraintSet.TOP,0);
set.connect(leftButton.getId(),ConstraintSet.BOTTOM,layout.getId(),ConstraintSet.BOTTOM,0);
set.connect(rightButton.getId(),ConstraintSet.LEFT,rightButton.getId(),ConstraintSet.RIGHT,0);
set.connect(rightButton.getId(),ConstraintSet.RIGHT,layout.getId(),ConstraintSet.RIGHT,0);
set.connect(rightButton.getId(),ConstraintSet.TOP,layout.getId(),ConstraintSet.TOP,0);
set.connect(rightButton.getId(),ConstraintSet.BOTTOM,layout.getId(),ConstraintSet.BOTTOM,0);
set.applyTo(layout);
}
}
And here is a screenshot of the above code: programmatically created.
Below is some xml that describes the same thing and works fine, for reference:
<ImageButton
android:layout_width="0dp"
android:layout_height="0dp"
android:id="@+id/right_button"
app:layout_constraintLeft_toRightOf="@+id/left_button"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
<ImageButton
android:layout_width="0dp"
android:layout_height="0dp"
android:id="@+id/left_button"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/right_button"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
Here is a screenshot of the xml: manually created
There's a couple of problems with what you are doing. First, you are assigning the same LayoutParams instance to both objects -- that won't work. You need a separate instance:
ConstraintLayout.LayoutParams anySizeParams2 = new ConstraintLayout.LayoutParams(0, 0);
rightButton.setLayoutParams(anySizeParams2);
Second, you got the connections wrong:
set.connect(rightButton.getId(),ConstraintSet.LEFT,
rightButton.getId(),ConstraintSet.RIGHT,0);
should be:
set.connect(rightButton.getId(),ConstraintSet.LEFT,
leftButton.getId(),ConstraintSet.RIGHT,0);
As otherwise it's not going to create a chain.
It probably would be cleaner to clone the layout after you added the views too.
这篇关于使用ConstraintLayout以编程方式连接设置为任意大小的多个视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!