ConstraintSet.RIGHT 不工作
ConstraintSet.RIGHT not working
我正在尝试创建一个可以在没有数据库的情况下将视图动态添加到 ConstraintLayout 的应用程序。我将 LayoutParams 包装在那里,以查看它们的去向。我已经能够添加视图,但我不知道为什么它们会继续移动到屏幕的左侧,并且即使我为每个视图使用不同的 LayoutParams,大小也相同。此外,边距似乎也不起作用。下面是我的代码:
ConstraintSet set = new ConstraintSet();
ConstraintLayout.LayoutParams lp = new ConstraintLayout.LayoutParams(
ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(8, 8, 8, 0);
ConstraintLayout.LayoutParams lpWrap = new ConstraintLayout.LayoutParams(
ConstraintLayout.LayoutParams.WRAP_CONTENT, ConstraintLayout.LayoutParams.WRAP_CONTENT);
int fieldCount = sharedPref.getInt(fieldCountTag, 0);
fieldCount++;
editor = sharedPref.edit();
editor.putInt(fieldCountTag, fieldCount);
editor.commit();
EditText txt = new EditText(ctx);
txt.setHint("IP:Port "+fieldCount);
txt.setInputType(InputType.TYPE_TEXT_VARIATION_WEB_EDIT_TEXT);
txt.setId(fieldCount);
txt.setLayoutParams(lp);
Button btn = new Button(ctx);
btn.setText("Use "+fieldCount);
btn.setId(fieldCount);
btn.setLayoutParams(lpWrap);
mainLyt.addView(txt);
mainLyt.addView(btn);
set.clone(mainLyt);
set.connect(btn.getId(), ConstraintSet.RIGHT, mainLyt.getId(), ConstraintSet.RIGHT, 8);
set.connect(btn.getId(), ConstraintSet.TOP, txt.getId(), ConstraintSet.TOP, 0);
set.connect(txt.getId(), ConstraintSet.LEFT, mainLyt.getId(), ConstraintSet.LEFT, 8);
set.connect(txt.getId(), ConstraintSet.TOP, txt3.getId(), ConstraintSet.BOTTOM, 8);
set.connect(txt.getId(), ConstraintSet.RIGHT, btn.getId(), ConstraintSet.LEFT, 8);
set.applyTo(mainLyt);
更新:
我想做的只是简单地并排添加一个编辑文本和按钮
更新:
我当前的代码:
ConstraintSet set = new ConstraintSet();
ConstraintLayout.LayoutParams lp = new ConstraintLayout.LayoutParams(
ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(8, 8, 8, 0);
ConstraintLayout.LayoutParams lpWrap = new ConstraintLayout.LayoutParams(
ConstraintLayout.LayoutParams.WRAP_CONTENT, ConstraintLayout.LayoutParams.WRAP_CONTENT);
int fieldCount = sharedPref.getInt(fieldCountTag, initFieldCount);
int topViewId = 0;
for(int x = 3; x < fieldCount; x++){
if(txtId != 0){
prevTxtId = txtId;
}
txtId = View.generateViewId();
btnId = View.generateViewId();
int id = x;
id++;
EditText txt = new EditText(ctx);
txt.setHint("IP:Port "+id);
txt.setInputType(InputType.TYPE_TEXT_VARIATION_WEB_EDIT_TEXT);
txt.setId(txtId);
txt.setLayoutParams(lp);
Button btn = new Button(ctx);
btn.setText("Use "+id);
btn.setId(btnId);
btn.setLayoutParams(lpWrap);
mainLyt.addView(txt);
mainLyt.addView(btn);
set.clone(mainLyt);
if(x == 3){
Log.d(TAG, "txt3Id: "+txt3.getId());
topViewId = txt3.getId();
}else{
topViewId = prevTxtId;
}
set.connect(btnId, ConstraintSet.END, mainLyt.getId(), ConstraintSet.END, 0);
set.connect(btnId, ConstraintSet.TOP, txtId, ConstraintSet.TOP, 0);
set.connect(txtId, ConstraintSet.START, mainLyt.getId(), ConstraintSet.START, 0);
set.connect(txtId, ConstraintSet.TOP, topViewId, ConstraintSet.BOTTOM, 0);
set.connect(txtId, ConstraintSet.END, btnId, ConstraintSet.START, 0);
set.applyTo(mainLyt);
}
更新:
我的加载方法:
ConstraintSet set = new ConstraintSet();
ConstraintLayout.LayoutParams lp = new ConstraintLayout.LayoutParams(
ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(8, 8, 8, 0);
ConstraintLayout.LayoutParams lpWrap = new ConstraintLayout.LayoutParams(
ConstraintLayout.LayoutParams.WRAP_CONTENT, ConstraintLayout.LayoutParams.WRAP_CONTENT);
int fieldCount = sharedPref.getInt(fieldCountTag, initFieldCount);
int topViewId = 0;
Log.d(TAG, "fieldCount: "+fieldCount);
Log.d(TAG, "------");
for(int x = 3; x < fieldCount; x++){
if(txtId != 0){
prevTxtId = txtId;
}
txtId = View.generateViewId();
btnId = View.generateViewId();
int id = x;
id++;
EditText txt = new EditText(ctx);
txt.setHint("IP:Port "+id);
txt.setInputType(InputType.TYPE_TEXT_VARIATION_WEB_EDIT_TEXT);
txt.setId(txtId);
txt.setLayoutParams(lp);
Button btn = new Button(ctx);
btn.setText("Use "+id);
btn.setId(btnId);
btn.setLayoutParams(lpWrap);
mainLyt.addView(txt);
mainLyt.addView(btn);
set.clone(mainLyt);
if(x == 3){
Log.d(TAG, "txt3Id: "+txt3.getId());
topViewId = txt3.getId();
}else{
topViewId = prevTxtId;
}
Log.d(TAG, "txtId: "+txtId+" connect to topView: "+topViewId+" end"+" btnId: "+btnId);
set.connect(btnId, ConstraintSet.END, mainLyt.getId(), ConstraintSet.END, 0);
set.connect(btnId, ConstraintSet.TOP, txtId, ConstraintSet.TOP, 0);
set.connect(txtId, ConstraintSet.START, mainLyt.getId(), ConstraintSet.START, 0);
set.connect(txtId, ConstraintSet.TOP, topViewId, ConstraintSet.BOTTOM, 0);
set.connect(txtId, ConstraintSet.END, btnId, ConstraintSet.START, 0);
prevTxtId = txtId;
set.applyTo(mainLyt);
}
更新:
我尝试在 xml 中使用 3 个 EditText 相互连接,但效果很好。逻辑与我的加载方法相同,但它仍然不起作用。意见不断上升。日志显示
txtId: 1 connect to topView: 2131558527 end btnId: 2 fieldCount: 4
txtId: 3 connect to topView: 1 end btnId: 4 fieldCount: 5
哪个应该是正确的。
更新:
这是我的xml如果你想试试
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mainLyt"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="app.com.simplewebview.MainActivity">
<EditText
android:id="@+id/txt1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:hint="IP:Port 1"
android:inputType="textUri"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/btn1"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:layout_marginTop="0dp"
android:text="Use 1"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="@+id/txt1" />
<EditText
android:id="@+id/txt2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:hint="IP:Port 2"
android:inputType="textUri"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/btn2"
app:layout_constraintTop_toBottomOf="@+id/txt1" />
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:layout_marginTop="0dp"
android:text="Use 2"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="@+id/txt2" />
<EditText
android:id="@+id/txt3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:hint="IP:Port 3"
android:inputType="textUri"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/btn3"
app:layout_constraintTop_toBottomOf="@+id/txt2" />
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:layout_marginTop="0dp"
android:text="Use 3"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="@+id/txt3" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginRight="8dp"
android:clickable="true"
app:fabSize="mini"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:srcCompat="@mipmap/ic_launcher" />
</android.support.constraint.ConstraintLayout>
txt
和 btn
需要有不同的 ID,否则约束将不起作用。如果您的 minSdkVersion
为 17 或更高,请尝试使用 View.generateViewId()
。无论您做什么,您的视图都需要不同的 ID。
一旦您确定您设置的 ID 是不同的,请检查您的约束。这些看起来不错,但不清楚容器中的垂直约束是什么,直到 txt3
具有垂直约束。另外,如果您仍然遇到问题,请尝试 start/end 而不是 left/right。
可能还有一些其他问题,但进行这些更改后,您应该会看到一些问题消失了。
我正在尝试创建一个可以在没有数据库的情况下将视图动态添加到 ConstraintLayout 的应用程序。我将 LayoutParams 包装在那里,以查看它们的去向。我已经能够添加视图,但我不知道为什么它们会继续移动到屏幕的左侧,并且即使我为每个视图使用不同的 LayoutParams,大小也相同。此外,边距似乎也不起作用。下面是我的代码:
ConstraintSet set = new ConstraintSet();
ConstraintLayout.LayoutParams lp = new ConstraintLayout.LayoutParams(
ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(8, 8, 8, 0);
ConstraintLayout.LayoutParams lpWrap = new ConstraintLayout.LayoutParams(
ConstraintLayout.LayoutParams.WRAP_CONTENT, ConstraintLayout.LayoutParams.WRAP_CONTENT);
int fieldCount = sharedPref.getInt(fieldCountTag, 0);
fieldCount++;
editor = sharedPref.edit();
editor.putInt(fieldCountTag, fieldCount);
editor.commit();
EditText txt = new EditText(ctx);
txt.setHint("IP:Port "+fieldCount);
txt.setInputType(InputType.TYPE_TEXT_VARIATION_WEB_EDIT_TEXT);
txt.setId(fieldCount);
txt.setLayoutParams(lp);
Button btn = new Button(ctx);
btn.setText("Use "+fieldCount);
btn.setId(fieldCount);
btn.setLayoutParams(lpWrap);
mainLyt.addView(txt);
mainLyt.addView(btn);
set.clone(mainLyt);
set.connect(btn.getId(), ConstraintSet.RIGHT, mainLyt.getId(), ConstraintSet.RIGHT, 8);
set.connect(btn.getId(), ConstraintSet.TOP, txt.getId(), ConstraintSet.TOP, 0);
set.connect(txt.getId(), ConstraintSet.LEFT, mainLyt.getId(), ConstraintSet.LEFT, 8);
set.connect(txt.getId(), ConstraintSet.TOP, txt3.getId(), ConstraintSet.BOTTOM, 8);
set.connect(txt.getId(), ConstraintSet.RIGHT, btn.getId(), ConstraintSet.LEFT, 8);
set.applyTo(mainLyt);
更新:
我想做的只是简单地并排添加一个编辑文本和按钮
更新: 我当前的代码:
ConstraintSet set = new ConstraintSet();
ConstraintLayout.LayoutParams lp = new ConstraintLayout.LayoutParams(
ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(8, 8, 8, 0);
ConstraintLayout.LayoutParams lpWrap = new ConstraintLayout.LayoutParams(
ConstraintLayout.LayoutParams.WRAP_CONTENT, ConstraintLayout.LayoutParams.WRAP_CONTENT);
int fieldCount = sharedPref.getInt(fieldCountTag, initFieldCount);
int topViewId = 0;
for(int x = 3; x < fieldCount; x++){
if(txtId != 0){
prevTxtId = txtId;
}
txtId = View.generateViewId();
btnId = View.generateViewId();
int id = x;
id++;
EditText txt = new EditText(ctx);
txt.setHint("IP:Port "+id);
txt.setInputType(InputType.TYPE_TEXT_VARIATION_WEB_EDIT_TEXT);
txt.setId(txtId);
txt.setLayoutParams(lp);
Button btn = new Button(ctx);
btn.setText("Use "+id);
btn.setId(btnId);
btn.setLayoutParams(lpWrap);
mainLyt.addView(txt);
mainLyt.addView(btn);
set.clone(mainLyt);
if(x == 3){
Log.d(TAG, "txt3Id: "+txt3.getId());
topViewId = txt3.getId();
}else{
topViewId = prevTxtId;
}
set.connect(btnId, ConstraintSet.END, mainLyt.getId(), ConstraintSet.END, 0);
set.connect(btnId, ConstraintSet.TOP, txtId, ConstraintSet.TOP, 0);
set.connect(txtId, ConstraintSet.START, mainLyt.getId(), ConstraintSet.START, 0);
set.connect(txtId, ConstraintSet.TOP, topViewId, ConstraintSet.BOTTOM, 0);
set.connect(txtId, ConstraintSet.END, btnId, ConstraintSet.START, 0);
set.applyTo(mainLyt);
}
更新: 我的加载方法:
ConstraintSet set = new ConstraintSet();
ConstraintLayout.LayoutParams lp = new ConstraintLayout.LayoutParams(
ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(8, 8, 8, 0);
ConstraintLayout.LayoutParams lpWrap = new ConstraintLayout.LayoutParams(
ConstraintLayout.LayoutParams.WRAP_CONTENT, ConstraintLayout.LayoutParams.WRAP_CONTENT);
int fieldCount = sharedPref.getInt(fieldCountTag, initFieldCount);
int topViewId = 0;
Log.d(TAG, "fieldCount: "+fieldCount);
Log.d(TAG, "------");
for(int x = 3; x < fieldCount; x++){
if(txtId != 0){
prevTxtId = txtId;
}
txtId = View.generateViewId();
btnId = View.generateViewId();
int id = x;
id++;
EditText txt = new EditText(ctx);
txt.setHint("IP:Port "+id);
txt.setInputType(InputType.TYPE_TEXT_VARIATION_WEB_EDIT_TEXT);
txt.setId(txtId);
txt.setLayoutParams(lp);
Button btn = new Button(ctx);
btn.setText("Use "+id);
btn.setId(btnId);
btn.setLayoutParams(lpWrap);
mainLyt.addView(txt);
mainLyt.addView(btn);
set.clone(mainLyt);
if(x == 3){
Log.d(TAG, "txt3Id: "+txt3.getId());
topViewId = txt3.getId();
}else{
topViewId = prevTxtId;
}
Log.d(TAG, "txtId: "+txtId+" connect to topView: "+topViewId+" end"+" btnId: "+btnId);
set.connect(btnId, ConstraintSet.END, mainLyt.getId(), ConstraintSet.END, 0);
set.connect(btnId, ConstraintSet.TOP, txtId, ConstraintSet.TOP, 0);
set.connect(txtId, ConstraintSet.START, mainLyt.getId(), ConstraintSet.START, 0);
set.connect(txtId, ConstraintSet.TOP, topViewId, ConstraintSet.BOTTOM, 0);
set.connect(txtId, ConstraintSet.END, btnId, ConstraintSet.START, 0);
prevTxtId = txtId;
set.applyTo(mainLyt);
}
更新:
我尝试在 xml 中使用 3 个 EditText 相互连接,但效果很好。逻辑与我的加载方法相同,但它仍然不起作用。意见不断上升。日志显示
txtId: 1 connect to topView: 2131558527 end btnId: 2 fieldCount: 4
txtId: 3 connect to topView: 1 end btnId: 4 fieldCount: 5
哪个应该是正确的。
更新:
这是我的xml如果你想试试
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mainLyt"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="app.com.simplewebview.MainActivity">
<EditText
android:id="@+id/txt1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:hint="IP:Port 1"
android:inputType="textUri"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/btn1"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:layout_marginTop="0dp"
android:text="Use 1"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="@+id/txt1" />
<EditText
android:id="@+id/txt2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:hint="IP:Port 2"
android:inputType="textUri"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/btn2"
app:layout_constraintTop_toBottomOf="@+id/txt1" />
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:layout_marginTop="0dp"
android:text="Use 2"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="@+id/txt2" />
<EditText
android:id="@+id/txt3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:hint="IP:Port 3"
android:inputType="textUri"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/btn3"
app:layout_constraintTop_toBottomOf="@+id/txt2" />
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:layout_marginTop="0dp"
android:text="Use 3"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="@+id/txt3" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginRight="8dp"
android:clickable="true"
app:fabSize="mini"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:srcCompat="@mipmap/ic_launcher" />
</android.support.constraint.ConstraintLayout>
txt
和 btn
需要有不同的 ID,否则约束将不起作用。如果您的 minSdkVersion
为 17 或更高,请尝试使用 View.generateViewId()
。无论您做什么,您的视图都需要不同的 ID。
一旦您确定您设置的 ID 是不同的,请检查您的约束。这些看起来不错,但不清楚容器中的垂直约束是什么,直到 txt3
具有垂直约束。另外,如果您仍然遇到问题,请尝试 start/end 而不是 left/right。
可能还有一些其他问题,但进行这些更改后,您应该会看到一些问题消失了。