ConstraintLayout - 水平打包项目而不将宽度设置为 match_parent
ConstraintLayout - pack items horizontally without setting width to match_parent
我想实现那个简单的视图
但是,当设置像
这样的约束时
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/upgradeTo"
style="@style/NewText.H2.Primary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:text="UPGRADE TO"
app:layout_constraintBaseline_toBaselineOf="@id/premium"
app:layout_constraintEnd_toStartOf="@+id/premium"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/premium"
style="@style/NewText.H2.Accent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:background="@drawable/round_rect_separator"
android:backgroundTint="#fff3f8"
android:paddingLeft="12dp"
android:paddingTop="4dp"
android:paddingRight="12dp"
android:paddingBottom="4dp"
android:text="PREMIUM"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/upgradeTo"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
我的观点没有打包,而是扩展了
通常,我在那种情况下使用 android:layout_width=match_parent
但是,在那种情况下我不能,因为我在 PREMIUM 上的彩色背景也会拉伸。
是否有另一种不使用 match_parent
来打包它们的方法?
或者唯一的方法是重构我的观点(PREMIUM 的背景作为单独的 ImageView
)。
您只需在 TextView 中添加以下行:
app:layout_constraintHorizontal_chainStyle=”packed”
有关详细信息,请访问此 link:
https://medium.com/@nomanr/constraintlayout-chains-4f3b58ea15bb
我想实现那个简单的视图
但是,当设置像
这样的约束时<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/upgradeTo"
style="@style/NewText.H2.Primary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:text="UPGRADE TO"
app:layout_constraintBaseline_toBaselineOf="@id/premium"
app:layout_constraintEnd_toStartOf="@+id/premium"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/premium"
style="@style/NewText.H2.Accent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:background="@drawable/round_rect_separator"
android:backgroundTint="#fff3f8"
android:paddingLeft="12dp"
android:paddingTop="4dp"
android:paddingRight="12dp"
android:paddingBottom="4dp"
android:text="PREMIUM"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/upgradeTo"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
我的观点没有打包,而是扩展了
通常,我在那种情况下使用 android:layout_width=match_parent
但是,在那种情况下我不能,因为我在 PREMIUM 上的彩色背景也会拉伸。
是否有另一种不使用 match_parent
来打包它们的方法?
或者唯一的方法是重构我的观点(PREMIUM 的背景作为单独的 ImageView
)。
您只需在 TextView 中添加以下行:
app:layout_constraintHorizontal_chainStyle=”packed”
有关详细信息,请访问此 link: https://medium.com/@nomanr/constraintlayout-chains-4f3b58ea15bb