繁体   English   中英

Android ConstraintLayout - 移动视图失败

[英]Android ConstraintLayout - moving View fails

我的目标是在 ConstraintLayout 中动态创建 TextView 网格。 我看过很多例子,但有一个我没有看到的问题。 我将任务分为两个步骤:

  • 创建视图
  • 定位视图

我似乎掌握了第一个但没有掌握第二个。 通常,如果 View 的 ConstraintSet 在 XML 中定义并在启动时定位,我可以修改它,但在我以编程方式创建它时永远不会。 当我创建视图时,我会小心设置 id、layout_width 和 layout_height。 我尝试创建新视图的方法:

  • 创建它并从新的 LayoutParameters 对象设置参数
  • 创建它并从另一个视图设置参数。
  • 从 XML 模板膨胀它。

activity_main.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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:id="@+id/activity_main" >

    <Button
        android:id="@+id/makeView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10sp"
        android:text="Make View"
        android:onClick="makeView"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/placeView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10sp"
        android:text="Place View"
        android:onClick="placeView"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toEndOf="@+id/makeView" />

    <TextView
        android:id="@+id/anchorView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20sp"
        android:layout_marginStart="20sp"
        android:text="View 00"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

package com.example.textviewonthefly;

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

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import java.util.Locale;

public class MainActivity extends AppCompatActivity {
    final String TAG = "DEBUGME";
    final int START = ConstraintSet.START;
    final int END = ConstraintSet.END;
    final int TOP = ConstraintSet.TOP;

    int counter = 0;
    View anchorView;
    TextView nextView;
    ConstraintLayout  mainLayout;

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

        mainLayout = (ConstraintLayout) findViewById(R.id.activity_main);
        anchorView = (TextView) findViewById(R.id.anchorView);
    }

    public void makeView(View view) {
        nextView = new TextView(this);
        nextView.setLayoutParams(anchorView.getLayoutParams());
        int nextViewId = nextView.generateViewId();
        nextView.setId(nextViewId);
        int ViewCount = ++counter;
        String newName = String.format(Locale.US, "View %02d", ViewCount);
        nextView.setText(newName);
        this.mainLayout.addView(nextView);
    }

    public void placeView(View view) {
        ConstraintSet set = new ConstraintSet();
        set.clone(this.mainLayout);
        set.clear(nextView.getId());
        set.connect(nextView.getId(), TOP, anchorView.getId(), TOP);
        set.connect(nextView.getId(), START, anchorView.getId(), END);
        set.applyTo(this.mainLayout);
        anchorView = nextView;
    }
}

在此处输入图像描述

解决了。

clear(nextView.getId()); 太激进了。 我用更具体的指令替换了该行:

clear(nextView.getId(), END);
clear(nextView.getId(), BOTTOM);

它现在按预期工作,稍作修改,我的应用程序在我添加和放置时创建了一个水平链:

[View 00][View 01][View 02]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM