如何在 ConstraintLayout 中以编程方式将视图限制为 parent

How to contrain a view programmatically to parent in ConstraintLayout

我想使用约束布局以编程方式创建 TextView(或任何其他视图)。 textView 在 XML:

中看起来像这样
<TextView
    android:id="@+id/textView_Test"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Test "
    android:textSize="19sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintVertical_bias="0.16"
    app:layout_constraintHorizontal_bias="0.02"
    app:layout_constraintTop_toTopOf="parent" />

所以它应该被限制在所有方面的 parents 然后我可以使用偏置参数动态地改变它的位置。我该如何实施?我查看了此处给出的答案 ,但 objects 不受 parents 的限制,也没有像我预期的那样使用垂直和水平偏差来动态定位它。知道这在片段中是否以及如何实现吗(我使用 Single-Activity Multiple-Fragments 方法)?

更新: 我现在可以通过编程方式在受限布局中插入自定义视图(而不是 TextView)。但是,使用代码 constraintSet.setHorizontalBias(view.getId(), 0.56f); 设置方向没有任何效果(不知道,我使用的是哪个数字)。自定义视图始终位于左上角。

这里可以看到整个片段的代码(我标明了重要部分):

public class Test extends Fragment implements Runnable {

    /*
    Game variables
     */

    public static final int DELAY_MILLIS = 100;
    public static final int TIME_OF_A_LEVEL_IN_SECONDS = 90;
    private int currentTimeLeftInTheLevel_MILLIS;
    private Handler handler = new Handler();
    private FragmentGameBinding binding;

    private boolean viewHasBeenCreated = false;


    public Test() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentGameBinding.inflate(inflater, container, false);
        container.getContext();
        viewHasBeenCreated = true;
        startRound();
        return binding.getRoot();


    }

    public void startRound () {
        currentTimeLeftInTheLevel_MILLIS =TIME_OF_A_LEVEL_IN_SECONDS * 1000;
        updateScreen();
        handler.postDelayed(this, 1000);

    }
    private void updateScreen() {
        binding.textViewTimeLeftValue.setText("" + currentTimeLeftInTheLevel_MILLIS/1000);

        /*
        IMPORTANT PART: This should create a simple custom UI element
         */
        View view = new View(getActivity());
        view.setLayoutParams(new ViewGroup.LayoutParams(200, 200));
        Drawable dr = ContextCompat.getDrawable(getActivity(),R.drawable.light_bulb_layer_list);
        view.setBackground(dr);
        view.setId(View.generateViewId());

        ConstraintLayout constraintLayout = binding.constraintLayout;
        ConstraintSet constraintSet = new ConstraintSet();
        constraintSet.clone(constraintLayout);
        constraintSet.connect(view.getId(),ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID,ConstraintSet.BOTTOM,0);
        constraintSet.connect(view.getId(),ConstraintSet.TOP,ConstraintSet.PARENT_ID ,ConstraintSet.TOP,0);
        constraintSet.connect(view.getId(),ConstraintSet.LEFT,ConstraintSet.PARENT_ID ,ConstraintSet.LEFT,0);
        constraintSet.connect(view.getId(),ConstraintSet.RIGHT,ConstraintSet.PARENT_ID ,ConstraintSet.RIGHT,0);
        constraintSet.setHorizontalBias(view.getId(), 0.56f);
        constraintSet.setVerticalBias(view.getId(), 0.5f);
        constraintSet.applyTo(constraintLayout);
        constraintLayout.addView(view);
    }

    private void countDownTime(){
        currentTimeLeftInTheLevel_MILLIS = currentTimeLeftInTheLevel_MILLIS -DELAY_MILLIS;
        updateScreen();
    }

    @Override
    public void run() {
        if(viewHasBeenCreated) {
            countDownTime();
        }

知道为什么在约束布局中设置水平或垂直方向没有任何效果吗?

根据文档,ConstraintSet 中有一个 PARENT_ID 用作约束的 ID。

同时设置 setHorizontalBiassetVerticalBias 方法。

您引用的 slashdot 问题的 向您展示了如何使用 ConstraintSet