Android ConstraintSet所有视图都具有相同的水平偏差

时间:2018-03-14 11:49:53

标签: android android-constraintlayout

我正在使用ConstraintSet约束我的应用中的两个视图。约束被正确创建,但是当我想设置水平偏差时,它会为我的ConstraintLayout中的所有视图设置它。这是我的代码:

cs.connect(img.getId(), ConstraintSet.RIGHT, ConstraintSet.PARENT_ID, ConstraintSet.RIGHT, 8);
cs.connect(img.getId(), ConstraintSet.LEFT, ConstraintSet.PARENT_ID, ConstraintSet.LEFT, 8);
cs.connect(img.getId(), ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP, 8);
cs.connect(img.getId(), ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM, 8);
cs.connect(text.getId(), ConstraintSet.RIGHT, ConstraintSet.PARENT_ID, ConstraintSet.RIGHT, 8);
cs.connect(text.getId(), ConstraintSet.LEFT, ConstraintSet.PARENT_ID, ConstraintSet.LEFT, 8);
cs.connect(text.getId(), ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP, 8);
cs.connect(text.getId(), ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM, 8);

//here is the problem, because in the end the bias is 0.026F for both views.
cs.setHorizontalBias(text.getId(), 0.9F);
cs.setHorizontalBias(img.getId(), 0.026F);

cs.applyTo(cl);

这是一个已知的错误,还是我做错了什么?我阅读了文档,它说它应该为给定的视图设置它:

"Adjust the horizontal bias of the view (used with views constrained on left and right)." 

我该怎么办?

1 个答案:

答案 0 :(得分:1)

你必须有别的错误,我刚刚测试了你的代码,它运行正常。文本设置在右侧,图像设置在左侧。

布局:

<?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/clConstraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ContaintLayoutActivity">

    <ImageView
        android:id="@+id/ivImage"
        android:layout_width="200dp"
        android:layout_height="150dp"
        android:src="@drawable/com_facebook_button_icon_blue"/>

    <TextView
        android:id="@+id/tvText"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:text="Este es un texto de prueba para este view"/>


</android.support.constraint.ConstraintLayout>

的活动:

public class ContaintLayoutActivity extends AppCompatActivity {
    ConstraintLayout cl;
    ImageView img;
    TextView text;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.constraint_with_progress);

        cl = (ConstraintLayout) findViewById(R.id.clConstraintLayout);
        img = (ImageView) findViewById(R.id.ivImage);
        text = (TextView) findViewById(R.id.tvText);

        ConstraintSet cs = new ConstraintSet();
        cs.clone(cl);

        cs.connect(img.getId(), ConstraintSet.RIGHT, ConstraintSet.PARENT_ID, ConstraintSet.RIGHT, 8);
        cs.connect(img.getId(), ConstraintSet.LEFT, ConstraintSet.PARENT_ID, ConstraintSet.LEFT, 8);
        cs.connect(img.getId(), ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP, 8);
        cs.connect(img.getId(), ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM, 8);
        cs.connect(text.getId(), ConstraintSet.RIGHT, ConstraintSet.PARENT_ID, ConstraintSet.RIGHT, 8);
        cs.connect(text.getId(), ConstraintSet.LEFT, ConstraintSet.PARENT_ID, ConstraintSet.LEFT, 8);
        cs.connect(text.getId(), ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP, 8);
        cs.connect(text.getId(), ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM, 8);

        cs.setHorizontalBias(text.getId(), 0.9F);
        cs.setHorizontalBias(img.getId(), 0.026F);

        cs.applyTo(cl);
    }
}

enter image description here

编辑我

当按照注释中的说明以编程方式添加视图时,您面临的最可能的问题是没有为视图设置id,在这种情况下两者都设置为-1,因此当您调用*时,两者都会获得相同的约束。的getId();

这是我开展工作的方式:

布局:

<?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/clConstraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ContaintLayoutActivity">


</android.support.constraint.ConstraintLayout>

res / values / ids.xml中的视图的ID:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item name="ivImage" type="id"/>
    <item name="tvText" type="id"/>
</resources>

的活动:

public class ContaintLayoutActivity extends AppCompatActivity {
    ConstraintLayout cl;
    ImageView img;
    TextView text;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.constraint_with_progress);

        cl = (ConstraintLayout) findViewById(R.id.clConstraintLayout);

        img = new ImageView(this);
        img.setId(R.id.ivImage);
        img.setLayoutParams(new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.WRAP_CONTENT,ConstraintLayout.LayoutParams.WRAP_CONTENT));
        img.setBackgroundColor(ContextCompat.getColor(this,R.color.colorPrimaryDark));
        img.setImageDrawable(ContextCompat.getDrawable(this,R.drawable.com_facebook_button_icon_blue));
        text = new TextView(this);
        text.setText("A text for testing");
        text.setLayoutParams(new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.WRAP_CONTENT,ConstraintLayout.LayoutParams.WRAP_CONTENT));
        text.setId(R.id.tvText);

        cl.addView(img);
        cl.addView(text);


        ConstraintSet cs = new ConstraintSet();
        cs.clone(cl);

        cs.connect(img.getId(), ConstraintSet.RIGHT, ConstraintSet.PARENT_ID, ConstraintSet.RIGHT, 8);
        cs.connect(img.getId(), ConstraintSet.LEFT, ConstraintSet.PARENT_ID, ConstraintSet.LEFT, 8);
        cs.connect(img.getId(), ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP, 8);
        cs.connect(img.getId(), ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM, 8);
        cs.connect(text.getId(), ConstraintSet.RIGHT, ConstraintSet.PARENT_ID, ConstraintSet.RIGHT, 8);
        cs.connect(text.getId(), ConstraintSet.LEFT, ConstraintSet.PARENT_ID, ConstraintSet.LEFT, 8);
        cs.connect(text.getId(), ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP, 8);
        cs.connect(text.getId(), ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM, 8);


        cs.setHorizontalBias(text.getId(), 0.9F);
        cs.setHorizontalBias(img.getId(), 0.026F);

        cs.applyTo(cl);

        cl.invalidate(); // May not be necessary
    }