【问题标题】:Android studio, Java: Cannot change horizontal margin at run time (ConstraintSet.LEFT)Android Studio,Java:无法在运行时更改水平边距(ConstraintSet.LEFT)
【发布时间】:2020-09-24 19:58:18
【问题描述】:

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

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

如果是它们都不起作用,我会考虑可能是我把它放在了错误的地方或什么地方,但是不,一个起作用,另一个不起作用,唯一的区别是一个有 TOP 和一个有 LEFT,实际上没有出错的余地。那么给了什么?这是测试运行的代码,我尝试了一个用 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>

【问题讨论】:

  • 如果只需要视觉上的改变,最好用animation和transitionY
  • 如果是带有功能的按钮,功能位置也会改变吗?
  • 它有效,谢谢。我还找到了另一个解决方案:``` ViewGroup.MarginLayoutParams margins = (ViewGroup.MarginLayoutParams) testLayout.getLayoutParams();边距.topMargin = 100;边距.leftMargin = 200; ```

标签: java android android-studio margin android-constraintlayout


【解决方案1】:

找到两个有效的答案,而不是使用 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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-26
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-12
    • 2014-06-05
    相关资源
    最近更新 更多