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

clu*_*ess 5 android android-layoutparams android-constraintlayout

我可以使用ConstraintSet在ConstraintLayout中以编程方式修改视图的约束,但是在应用修改之前,我想测试它是否尚未完成。为此,我需要以编程方式访问ConstraintLayout中视图的当前约束。

例如,在这里我想删除TextView“标题”的顶部和TextView视图“ subtitle”的底部之间的约束。然后将TextView“标题”的顶部约束到“ videoView”的底部。但是首先,我需要检查TextView的“标题”是否尚未限制在“ 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
    }
}
Run Code Online (Sandbox Code Playgroud)

}

<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"
    />
Run Code Online (Sandbox Code Playgroud)

Coc*_*chi 7

我承认,没有办法做到这一点,但是您可以尝试比较两个 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
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助。

  • 非常感谢!从视图的 ConstraintLayout.LayoutParams 调用 topToBottom 告诉我被限制在它的顶部的视图的 ID。文档 [此处](https://developer.android.com/reference/android/support/constraint/ConstraintLayout.LayoutParams.html#topToBottom) (2认同)