動態調整 ConstraintLayout === * 範例 ```xml= <TextView android:id="@+id/title" android:layout_height="wrap_content" android:layout_width="wrap_content" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"/> <ImageView android:id="@+id/image" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/title" /> ``` * 實作範例 in `xml`: `app:layout_constraintTop_toBottomOf=”@id/title”`,意義同下方; in code progamming: ```java= ConnectConstraint(R.id.image, ConstraintSet.Top, R.id.title, ConstraintSet.BOTTOM) ``` ```java= ConstraintSet set = new ConstraintSet(); ConstraintLayout layout; // 注意!! // 這裡的 R.id.layout 指的是整個佈局檔的最上層的根節點, 且應為 ConstraintLayout layout = (ConstraintLayout) findViewById(R.id.layout); set.clone(layout); // The following breaks the connection. set.clear(R.id.bottomText, ConstraintSet.TOP); // Comment out line above and uncomment line below to make the connection. // set.connect(R.id.bottomText, ConstraintSet.TOP, R.id.imageView, ConstraintSet.BOTTOM, 0); set.applyTo(layout); ``` ## Ref. [](https://medium.com/geekculture/programatically-update-view-constraints-in-constraint-layout-f4eb1b6dfc38) [Removing/Adding constraint programmatically in ConstraintLayout](https://stackoverflow.com/questions/44685816/removing-adding-constraint-programmatically-in-constraintlayout/44700437) ###### tags: `實作相關`
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up