如何以编程方式访问ConstraintLayout中视图的当前约束条件?

10
我可以使用ConstraintSet在ConstraintLayout中编程修改视图的约束条件,但在应用修改之前,我希望测试它是否已经完成。为此,我需要以编程方式访问ConstraintLayout中视图的当前约束条件。
例如,在这里,我想要删除TextView“title”顶部与TextView“subtitle”底部之间的约束条件。然后将TextView“title”的顶部限制为“videoView”的底部。但首先,我需要检查TextView“title”是否已经受到“videoView”的底部约束条件的限制(代码修改自android官方网站)。请注意,我无法在此网站上插入HTML的最后一行,它是用尖括号括起来的/android.support.constraint.ConstraintLayout。
public class MainActivity extends AppCompatActivity {
ConstraintSet mConstraintSet = new ConstraintSet(); // create a Constraint Set
ConstraintLayout mConstraintLayout; // cache the ConstraintLayout

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mConstraintLayout = (ConstraintLayout) findViewById(R.id.root);
    mConstraintSet.clone(mConstraintLayout); // get constraints from ConstraintSet
}

public void userDoesSomething(View v) {
    //test if title has already been constrained to bottom of videoView
    if (mConstraintLayout 'TITLE' IS NOT CONSTRAINED TO THE BOTTOM OF 'VIDEOVIEW'){
        mConstraintSet.connect(R.id.Title, ConstraintSet.TOP, R.id.videoView, ConstraintSet.BOTTOM); // set new constraints to ConstraintSet
        mConstraintSet.applyTo(mConstraintLayout); // set new constraints to ConstraintLayout
    }
}

}

<android.support.constraint.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/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.test.MainActivity"
android:orientation="vertical">

<VideoView
    android:id="@+id/videoView"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"

    />

<TextView
    android:id="@+id/subtitiles"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintTop_toBottomOf="@+id/videoView"
    app:layout_constraintLeft_toLeftOf="parent"
    android:gravity="center"
    android:text="The problem with this pond is that there are too many toads"
    android:textSize="25sp"
    android:background="@android:color/white"

    />

<TextView
    android:id="@+id/title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintTop_toBottomOf="@+id/subtitiles"
    app:layout_constraintLeft_toLeftOf="parent"
    android:gravity="center"
    android:text="Toad"
    android:textSize="25sp"
    android:background="@android:color/white"
    />


不太清楚你在问什么。 - rakwaht
@rakwaht。视图@+id/title被限制在视图@+id/subtitiles中。我想以类似于使用view.getHeight()访问高度的方式从我的MainActivity中访问此信息。这可能吗? - clueless
只需查看官方文档,它们甚至有一个示例。https://developer.android.com/reference/android/support/constraint/ConstraintSet.html - Martin Marconcini
@Martin Marconcini。我使用了相同的链接来编写我的问题示例。我没有看到它向我展示如何查找视图的约束条件。例如(如我在上面的评论中所说),您可以看到在我的布局中,@+id/title被限制为视图@+id/subtitiles。我希望能够以类似于使用view.getHeight()访问高度的方式从我的MainActivity访问此信息。 - clueless
1
它完美地展示了如何做到这一点,因为通过克隆ConstrainSet,并通过扩展获得任何小部件的布局参数,您可以访问其所有信息,包括但不限于您要查找的“topToBottom”值,根据接受的答案。您询问“需要以编程方式访问ConstraintLayout中视图的当前约束”。您克隆或获取布局参数,将它们转换为正确的类型,然后就可以访问其值。 - Martin Marconcini
显示剩余2条评论
1个回答

9
据我所知,目前没有这方面的方法。但是你可以尝试比较两个ConstraintLayout.LayoutParams对象,例如:
public class MainActivity extends AppCompatActivity {
ConstraintSet mConstraintSet = new ConstraintSet(); // create a Constraint Set
ConstraintLayout mConstraintLayout; // cache the ConstraintLayout

int mConstraintValue;
TextView mTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mConstraintLayout = (ConstraintLayout) findViewById(R.id.root);
        mTextView = (TextView) findViewById(R.id.title);
        mConstraintSet.clone(mConstraintLayout); // get constraints from ConstraintSet
        ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) mTextView.getLayoutParams();
        mConstraintValue = params.topToBottom;
    }

    public void userDoesSomething(View v) {
        //test if title has already been constrained to bottom of videoView
         ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) mTextView.getLayoutParams();
        if (mConstraintValue.equals(params.topToBottom)){
            mConstraintSet.connect(R.id.Title, ConstraintSet.TOP, R.id.videoView, ConstraintSet.BOTTOM); // set new constraints to ConstraintSet
            mConstraintSet.applyTo(mConstraintLayout); // set new constraints to ConstraintLayout
        }
    }
}

希望这有所帮助。

2
非常感谢!从视图的ConstraintLayout.LayoutParams调用topToBottom告诉我与其顶部有约束的视图的ID。文档在这里 - clueless

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接