Android studio,Java:无法在运行时更改水平边距(ConstraintSet.LEFT)

问题描述 投票:0回答:1

正如标题所述,我通常在运行时使用ConstraintSet来更改按钮或布局的位置,并且在可以工作的地方有很长的代码,但是我在同一项目中再次尝试,然后在测试项目中再次尝试(下面),而我唯一能改变的就是垂直边距。由于某些奇怪的原因,水平边距被忽略了。该行具体为:

set.connect(testLayout.getId(), ConstraintSet.LEFT, rootLayout.getId(),ConstraintSet.LEFT, 100);

[如果是两个都不起作用,我会考虑也许我把它放在错误的地方或其他地方,但是不,一个起作用而另一个却不起作用,唯一的区别是一个具有TOP而一个具有TOP左,实际上没有错误的余地。那有什么呢?这是测试运行的代码,我尝试了一种用xml设计的程序,以及一种以编程方式设计的程序,但都不适用于LEFT情况:

主要

package com.example.cusom_buttom_test1;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.constraintlayout.widget.ConstraintSet;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //XML OBJECTS
        ConstraintLayout rootLayout = findViewById(R.id.rootLayout);
        ConstraintLayout testLayout = findViewById(R.id.relativeLayout_test);

        //SET LOCATION OF testLayout
            ConstraintSet set = new ConstraintSet();
            set.clone(rootLayout);
            set.connect(testLayout.getId(), ConstraintSet.TOP, rootLayout.getId(),ConstraintSet.TOP, 500);
            set.connect(testLayout.getId(), ConstraintSet.LEFT, rootLayout.getId(),ConstraintSet.LEFT, 100);
            set.applyTo(rootLayout);

        //CREATE NEW CONSTRAINT LAYOUT
            ConstraintLayout testLayout2 = new ConstraintLayout(this);
            testLayout2.setId(View.generateViewId());
            testLayout2.setBackgroundColor(Color.GREEN);
            rootLayout.addView(testLayout2);

            //SET SIZE
            testLayout2.getLayoutParams().height = 200;
            testLayout2.getLayoutParams().width = 200;

            //SET LOCATION
            ConstraintSet set2 = new ConstraintSet();
            set2.clone(rootLayout);
            set2.connect(testLayout2.getId(), ConstraintSet.TOP, rootLayout.getId(), ConstraintSet.TOP, 100);
            set2.connect(testLayout.getId(), ConstraintSet.LEFT, rootLayout.getId(), ConstraintSet.LEFT, 300);
            set2.applyTo(rootLayout);
    }
}

XML

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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/rootLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/relativeLayout_test"
        android:layout_width="222dp"
        android:layout_height="230dp"
        android:background="#DD1515"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>
java android android-studio margin android-constraintlayout
1个回答
0
投票

找到了两个有效的答案,而不是使用ConstraintSet,请使用animate或MarginLayoutParams。

testLayout.animate().x(300).y(700).setDuration(0).start();
testLayout.animate().x(300).y(700).setDuration(0).start();
ViewGroup.MarginLayoutParams margins = (ViewGroup.MarginLayoutParams) testLayout.getLayoutParams();
margins.topMargin = 100;
margins.leftMargin = 200;
© www.soinside.com 2019 - 2024. All rights reserved.