【发布时间】:2018-01-27 07:10:11
【问题描述】:
我可以使用 ConstraintSet 以编程方式修改 ConstraintLayout 中视图的约束,但在应用修改之前,我想测试它是否已经完成。为此,我需要以编程方式访问 ConstraintLayout 中视图的当前约束。
例如,这里我想移除 TextView 'title' 顶部和 TextView 视图 'subtitle' 底部之间的约束。然后将 TextView 'title' 的顶部约束到 'videoView' 的底部。但首先我需要检查 TextView 'title' 是否已经被限制在 'videoView' 的底部(代码从 android's official website 修改)。注意,我不能在这个网站上插入 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。视图@+id/title 受限于视图@+id/subtitiles。我希望能够以与使用 view.getHeight() 访问高度类似的方式从 MainActivity 访问此信息。有可能吗?
-
看看官方文档,他们甚至有一个例子。 developer.android.com/reference/android/support/constraint/…
-
@Martin Marconcini。我使用相同的链接在我的问题中编写示例。我看不到它在哪里向我展示了如何找出视图的约束。例如(正如我在上面的评论中所说),您可以看到在我的布局中@+id/title 被限制为视图@+id/subtitiles。我希望能够以与使用 view.getHeight() 访问高度类似的方式从 MainActivity 访问此信息。
-
它确实完美地向您展示了如何做到这一点,因为通过克隆 ConstrainSet 并通过扩展获取任何小部件的布局参数,您可以访问其所有信息,包括但不限于根据接受的答案,您正在寻找的是
topToBottom值。您问“需要以编程方式访问 ConstraintLayout 中视图的当前约束”。您克隆或获取布局参数,将它们转换为正确的类型,并且可以访问其值。
标签: android android-constraintlayout android-layoutparams