gpt4 book ai didi

android - 将动态 subview 垂直添加到 ConstraintLayout

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:17:24 25 4
gpt4 key购买 nike

Android ViewGroup 具有以下添加(子) View 的方法:

enter image description here

我知道我们可以轻松地在 xml 中/以编程方式为 LinearLayout 定义水平/垂直方向,并添加 subview ,例如。

enter image description here

与RelativeLayout类似,我们可以使用ViewGroupaddViewRelativeLayout.LayoutParams的方法:

enter image description here

我还知道我们可以在 ConstraintLayout 中使用 LinearLayout 作为子项并使用它。

是否有任何可靠的推荐方法可以动态地将 subview 添加到 ConstraintLayout?

更新:让我举一个我想要实现的不太简单的例子。

假设您有一个View1、View2 和View3。所有 V1、V2 和 V3 都在垂直方向上依次排列,并且是由多个 TextViewImageView 组成的复杂 View 。根据用户操作和服务器发送的信息,我需要在原始 V2 和 V3 之间添加多个 V1 和 V2(可以是 1 对 V1-V2,也可以是 3 对 V1-V2)。如果我正在使用 ConstraintLayout,当我可以轻松地使用垂直方向的 LinearLayout 时,最好以编程方式添加多个约束吗?

现在,就效率、性能和简洁美观的代码而言,与线性布局相比,ConstraintLayout 是否最适合此要求?

最佳答案

可以像使用 LinearLayout 一样使用 addView() 将 View 添加到 ConstraintLayout。不同之处在于,使用 ConstraintLayout 时,必须限制添加的 View 。要以编程方式约束 View ,请使用 ConstraintSet .

This class allows you to define programmatically a set of constraints to be used with ConstraintLayout. It lets you create and save constraints, and apply them to an existing ConstraintLayout.

这是一个简单的例子:

activity_main
定义两个 TextViews。它们水平居中并位于顶部。

<android.support.constraint.ConstraintLayout 
android:id="@+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:id="@+id/topView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Top View"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/bottomView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Bottom View"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/topView" />

</android.support.constraint.ConstraintLayout>

这是在未设置约束的情况下将新的 TextView(“中间 View ”)添加到此布局时您将看到的内容。请注意,新 View 默认位于位置 (0,0)。

enter image description here

假设我们希望生成的中间 View 像这样在窗口中水平居中放置在顶 View 和底 View 之间:

enter image description here

这是将产生此结果的代码:

MainActivity.java

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

// Define the new TextView and add it to the ConstraintLayout. Without constraints,
// this view will be positioned at (0,0).
TextView middleView = new TextView(this);
middleView.setId(View.generateViewId());
middleView.setText("Middle View");
middleView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20.0f);
ConstraintLayout layout = findViewById(R.id.constraintLayout);
ConstraintLayout.LayoutParams lp =
new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.WRAP_CONTENT,
ConstraintLayout.LayoutParams.WRAP_CONTENT);
layout.addView(middleView, lp);

// Move the new view into place by applying constraints.
ConstraintSet set = new ConstraintSet();
// Get existing constraints. This will be the base for modification.
set.clone(layout);
int topMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
16, getResources().getDisplayMetrics());
// Set up the connections for the new view. Constrain its top to the bottom of the top view.
set.connect(middleView.getId(), ConstraintSet.TOP, R.id.topView, ConstraintSet.BOTTOM, topMargin);
// Constrain the top of the bottom view to the bottom of the new view. This will replace
// the constraint from the bottom view to the bottom of the top view.
set.connect(R.id.bottomView, ConstraintSet.TOP, middleView.getId(), ConstraintSet.BOTTOM, topMargin);
// Since views must be constrained vertically and horizontally, establish the horizontal
// constaints such that the new view is centered.
set.centerHorizontally(middleView.getId(),ConstraintSet.PARENT_ID);
// Finally, apply our good work to the layout.
set.applyTo(layout);
}

关于android - 将动态 subview 垂直添加到 ConstraintLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52492674/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com