【发布时间】:2023-01-18 06:51:48
【问题描述】:
我有一个 ConstraintLayout 包含许多元素,包括网格布局中的几个按钮。这是布局的简化版本(实际布局包括其他元素):
<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">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="0dp"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Button 1"
app:layout_constraintBottom_toTopOf="@+id/button3"
app:layout_constraintEnd_toStartOf="@+id/button2"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toolbar" />
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Button 2"
app:layout_constraintBottom_toTopOf="@+id/button4"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/button1"
app:layout_constraintTop_toBottomOf="@+id/toolbar" />
<Button
android:id="@+id/button3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Button 3"
app:layout_constraintBottom_toTopOf="@id/guideline"
app:layout_constraintEnd_toStartOf="@+id/button4"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button1" />
<Button
android:id="@+id/button4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Button 4"
app:layout_constraintBottom_toTopOf="@id/guideline"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/button3"
app:layout_constraintTop_toBottomOf="@+id/button2" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.5" />
</androidx.constraintlayout.widget.ConstraintLayout>
我希望按钮的大小满足两个条件:
- 每个按钮都应包含其所有文本,没有任何内容被截断或省略
- 所有按钮的大小应该相同
换句话说,每个按钮的大小应该等于任何按钮的最大文本的大小。是否可以在
ConstraintLayout中执行此操作?我可以通过将按钮宽度和高度分别设置为
odp和wrap_content来满足第一个条件(如在上面的 XML 中),这适用于所有按钮文本大小相同的特定情况(如在 XML 之上,并且在我的应用程序的原始(英文)版本中),但是当它们不是时(例如,Button 4 plus several extra words被替换为Button 4,并且至少出现在我的应用程序的一个翻译中应用程序),然后按钮大小和对齐方式会失去对称性和均匀性。我想坚持使用
ConstraintLayout,最好不要在其中嵌套另一个布局,因为这似乎是今天推荐的做事方式,但我想如果必须的话我可以切换。我已经尝试了 here 的一些想法,但我无法让它们在我的案例中起作用,正如我所说,如果可能的话,我真的宁愿坚持使用纯粹的ConstraintLayout。
【问题讨论】:
标签: android android-layout android-constraintlayout android-xml