Java 如何以编程方式创建约束布局
我不熟悉约束布局,很难弄清楚如何以编程方式创建它 以下是我的xml代码:Java 如何以编程方式创建约束布局,java,android,android-layout,android-fragments,android-constraintlayout,Java,Android,Android Layout,Android Fragments,Android Constraintlayout,我不熟悉约束布局,很难弄清楚如何以编程方式创建它 以下是我的xml代码: <RelativeLayout android:id="@+id/ShareContainer" android:layout_width="0dp" android:layout_height="0dp" android:layout_marginTop="0dp" android:layout_marginBottom="0dp"
<RelativeLayout
android:id="@+id/ShareContainer"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="0dp"
android:layout_marginBottom="0dp"
app:layout_constraintDimensionRatio="16:9"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/StatusContainer"
app:layout_constraintBottom_toTopOf="@+id/ButtonContainer">
<com.myapp.ui.testLayout
android:id="@+id/ShareVideo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black"/>
<ImageButton
android:id="@+id/ShareToggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="36dp"
android:layout_marginLeft="16dp"
android:background="@android:color/transparent"
android:src="@drawable/ic_video_cam_switch" />
</RelativeLayout>
可以通过编程方式创建约束布局,类似于创建任何其他布局。 此外,还可以使用以编程方式设置约束 就你而言:
ConstraintSet set = new ConstraintSet (context);
int id = R.id.ShareContainer, root_id=R.id.root_container; // I don't know root container id, so suppose it is root_container
// ratio
set.setDimensionRatio(id, "16:9");
//SIDE to SIDE of VIEW
set.connect(id, BOTTOM, root_id, BOTTOM, 8); //object, side, anchor, anchor's side, margin
set.applyTo(contraintLayout);
XML代码:
app:layout_constraintTop_toBottomOf=“@id/furnishTypeGroup”
代码
val layoutParams = myView.getLayoutParams() as ConstraintLayout.LayoutParams
layoutParams.topToBottom = R.id.furnishTypeGroup
myView.setLayoutParams(layoutParams)
我正在尝试以编程方式添加约束布局。您能解释一下我的代码吗?作为一个示例,这将有助于理解
val layoutParams = myView.getLayoutParams() as ConstraintLayout.LayoutParams
layoutParams.topToBottom = R.id.furnishTypeGroup
myView.setLayoutParams(layoutParams)