按钮不会填满整个 space
Buttons won't fill the whole space
我希望我的布局显示 6 个均匀填充整个屏幕的按钮,但目前每个按钮仅填充其填充。
这 6 个按钮显示在 ScrollView 内的 ConstraintLayout 内,以便在任何设备上很好地显示布局。
这是我的布局:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
tools:context=".MainActivity" >
<android.support.constraint.ConstraintLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/idbutton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BUTTON1"
app:layout_constraintBottom_toTopOf="@+id/idbutton3"
app:layout_constraintEnd_toStartOf="@+id/idbutton2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:background="#f0eb79"
android:foreground="?attr/selectableItemBackground"
android:drawableTop="@drawable/default_icon"
android:textAllCaps="false"
android:textColor="#ffffff"
android:textSize="18sp"
android:padding="25dp" />
<Button
android:id="@+id/idbutton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BUTTON2"
app:layout_constraintBottom_toTopOf="@+id/idbutton4"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/idbutton1"
app:layout_constraintTop_toTopOf="parent"
android:background="#000"
android:foreground="?attr/selectableItemBackground"
android:drawableTop="@drawable/default_icon"
android:textAllCaps="false"
android:textColor="#ffffff"
android:textSize="18sp"
android:padding="25dp" />
<Button
android:id="@+id/idbutton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BUTTON3"
app:layout_constraintEnd_toStartOf="@+id/idbutton4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/idbutton1"
app:layout_constraintBottom_toTopOf="@+id/idbutton5"
android:background="#f0eb79"
android:foreground="?attr/selectableItemBackground"
android:drawableTop="@drawable/default_icon"
android:textAllCaps="false"
android:textColor="#ffffff"
android:textSize="18sp"
android:padding="25dp" />
<Button
android:id="@+id/idbutton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BUTTON4"
app:layout_constraintBottom_toTopOf="@+id/idAppointments"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/idbutton3"
app:layout_constraintTop_toBottomOf="@+id/idbutton2"
android:background="#000"
android:foreground="?attr/selectableItemBackground"
android:drawableTop="@drawable/default_icon"
android:textAllCaps="false"
android:textColor="#ffffff"
android:textSize="18sp"
android:padding="25dp" />
<Button
android:id="@+id/idbutton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BUTTON5"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/idAppointments"
app:layout_constraintTop_toBottomOf="@+id/idbutton3"
app:layout_constraintStart_toStartOf="parent"
android:background="#f0eb79"
android:foreground="?attr/selectableItemBackground"
android:drawableTop="@drawable/default_icon"
android:textAllCaps="false"
android:textColor="#ffffff"
android:textSize="18sp"
android:padding="25dp" />
<Button
android:id="@+id/idAppointments"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BUTTON6"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/idbutton5"
app:layout_constraintTop_toBottomOf="@+id/idbutton4"
android:background="#000"
android:foreground="?attr/selectableItemBackground"
android:drawableTop="@drawable/default_icon"
android:textAllCaps="false"
android:textColor="#ffffff"
android:textSize="18sp"
android:padding="25dp" />
</android.support.constraint.ConstraintLayout>
</ScrollView>
这是我用上面的代码得到的结果:
https://i.imgur.com/7mH0sZy.png
正如您在按钮周围看到的那样,我不想要 space。有没有办法让按钮填满整个space?
尝试使用下面的代码(在此示例中我删除了 android:drawableTop="@drawable/default_icon" 但只需重新添加)。
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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/scrollView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
tools:context=".MainActivity"
>
<android.support.constraint.ConstraintLayout xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/idbutton1"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#f0eb79"
android:foreground="?attr/selectableItemBackground"
android:padding="25dp"
android:text="BUTTON1"
android:textAllCaps="false"
android:textColor="#ffffff"
android:textSize="18sp"
app:layout_constraintBottom_toTopOf="@+id/idbutton3"
app:layout_constraintEnd_toStartOf="@+id/idbutton2"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/idbutton2"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#000"
android:foreground="?attr/selectableItemBackground"
android:padding="25dp"
android:text="BUTTON2"
android:textAllCaps="false"
android:textColor="#ffffff"
android:textSize="18sp"
app:layout_constraintBottom_toTopOf="@+id/idbutton4"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/idbutton1"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/idbutton3"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#f0eb79"
android:foreground="?attr/selectableItemBackground"
android:padding="25dp"
android:text="BUTTON3"
android:textAllCaps="false"
android:textColor="#ffffff"
android:textSize="18sp"
app:layout_constraintBottom_toTopOf="@+id/idbutton5"
app:layout_constraintEnd_toStartOf="@+id/idbutton4"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/idbutton1" />
<Button
android:id="@+id/idbutton4"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#000"
android:foreground="?attr/selectableItemBackground"
android:padding="25dp"
android:text="BUTTON4"
android:textAllCaps="false"
android:textColor="#ffffff"
android:textSize="18sp"
app:layout_constraintBottom_toTopOf="@+id/idAppointments"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/idbutton3"
app:layout_constraintTop_toBottomOf="@+id/idbutton2" />
<Button
android:id="@+id/idbutton5"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#f0eb79"
android:foreground="?attr/selectableItemBackground"
android:padding="25dp"
android:text="BUTTON5"
android:textAllCaps="false"
android:textColor="#ffffff"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/idAppointments"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/idbutton3" />
<Button
android:id="@+id/idAppointments"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#000"
android:foreground="?attr/selectableItemBackground"
android:padding="25dp"
android:text="BUTTON6"
android:textAllCaps="false"
android:textColor="#ffffff"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/idbutton5"
app:layout_constraintTop_toBottomOf="@+id/idbutton4" />
</android.support.constraint.ConstraintLayout>
我希望我的布局显示 6 个均匀填充整个屏幕的按钮,但目前每个按钮仅填充其填充。
这 6 个按钮显示在 ScrollView 内的 ConstraintLayout 内,以便在任何设备上很好地显示布局。
这是我的布局:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
tools:context=".MainActivity" >
<android.support.constraint.ConstraintLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/idbutton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BUTTON1"
app:layout_constraintBottom_toTopOf="@+id/idbutton3"
app:layout_constraintEnd_toStartOf="@+id/idbutton2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:background="#f0eb79"
android:foreground="?attr/selectableItemBackground"
android:drawableTop="@drawable/default_icon"
android:textAllCaps="false"
android:textColor="#ffffff"
android:textSize="18sp"
android:padding="25dp" />
<Button
android:id="@+id/idbutton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BUTTON2"
app:layout_constraintBottom_toTopOf="@+id/idbutton4"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/idbutton1"
app:layout_constraintTop_toTopOf="parent"
android:background="#000"
android:foreground="?attr/selectableItemBackground"
android:drawableTop="@drawable/default_icon"
android:textAllCaps="false"
android:textColor="#ffffff"
android:textSize="18sp"
android:padding="25dp" />
<Button
android:id="@+id/idbutton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BUTTON3"
app:layout_constraintEnd_toStartOf="@+id/idbutton4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/idbutton1"
app:layout_constraintBottom_toTopOf="@+id/idbutton5"
android:background="#f0eb79"
android:foreground="?attr/selectableItemBackground"
android:drawableTop="@drawable/default_icon"
android:textAllCaps="false"
android:textColor="#ffffff"
android:textSize="18sp"
android:padding="25dp" />
<Button
android:id="@+id/idbutton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BUTTON4"
app:layout_constraintBottom_toTopOf="@+id/idAppointments"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/idbutton3"
app:layout_constraintTop_toBottomOf="@+id/idbutton2"
android:background="#000"
android:foreground="?attr/selectableItemBackground"
android:drawableTop="@drawable/default_icon"
android:textAllCaps="false"
android:textColor="#ffffff"
android:textSize="18sp"
android:padding="25dp" />
<Button
android:id="@+id/idbutton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BUTTON5"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/idAppointments"
app:layout_constraintTop_toBottomOf="@+id/idbutton3"
app:layout_constraintStart_toStartOf="parent"
android:background="#f0eb79"
android:foreground="?attr/selectableItemBackground"
android:drawableTop="@drawable/default_icon"
android:textAllCaps="false"
android:textColor="#ffffff"
android:textSize="18sp"
android:padding="25dp" />
<Button
android:id="@+id/idAppointments"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BUTTON6"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/idbutton5"
app:layout_constraintTop_toBottomOf="@+id/idbutton4"
android:background="#000"
android:foreground="?attr/selectableItemBackground"
android:drawableTop="@drawable/default_icon"
android:textAllCaps="false"
android:textColor="#ffffff"
android:textSize="18sp"
android:padding="25dp" />
</android.support.constraint.ConstraintLayout>
</ScrollView>
这是我用上面的代码得到的结果:
https://i.imgur.com/7mH0sZy.png
正如您在按钮周围看到的那样,我不想要 space。有没有办法让按钮填满整个space?
尝试使用下面的代码(在此示例中我删除了 android:drawableTop="@drawable/default_icon" 但只需重新添加)。
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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/scrollView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
tools:context=".MainActivity"
>
<android.support.constraint.ConstraintLayout xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/idbutton1"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#f0eb79"
android:foreground="?attr/selectableItemBackground"
android:padding="25dp"
android:text="BUTTON1"
android:textAllCaps="false"
android:textColor="#ffffff"
android:textSize="18sp"
app:layout_constraintBottom_toTopOf="@+id/idbutton3"
app:layout_constraintEnd_toStartOf="@+id/idbutton2"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/idbutton2"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#000"
android:foreground="?attr/selectableItemBackground"
android:padding="25dp"
android:text="BUTTON2"
android:textAllCaps="false"
android:textColor="#ffffff"
android:textSize="18sp"
app:layout_constraintBottom_toTopOf="@+id/idbutton4"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/idbutton1"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/idbutton3"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#f0eb79"
android:foreground="?attr/selectableItemBackground"
android:padding="25dp"
android:text="BUTTON3"
android:textAllCaps="false"
android:textColor="#ffffff"
android:textSize="18sp"
app:layout_constraintBottom_toTopOf="@+id/idbutton5"
app:layout_constraintEnd_toStartOf="@+id/idbutton4"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/idbutton1" />
<Button
android:id="@+id/idbutton4"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#000"
android:foreground="?attr/selectableItemBackground"
android:padding="25dp"
android:text="BUTTON4"
android:textAllCaps="false"
android:textColor="#ffffff"
android:textSize="18sp"
app:layout_constraintBottom_toTopOf="@+id/idAppointments"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/idbutton3"
app:layout_constraintTop_toBottomOf="@+id/idbutton2" />
<Button
android:id="@+id/idbutton5"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#f0eb79"
android:foreground="?attr/selectableItemBackground"
android:padding="25dp"
android:text="BUTTON5"
android:textAllCaps="false"
android:textColor="#ffffff"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/idAppointments"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/idbutton3" />
<Button
android:id="@+id/idAppointments"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#000"
android:foreground="?attr/selectableItemBackground"
android:padding="25dp"
android:text="BUTTON6"
android:textAllCaps="false"
android:textColor="#ffffff"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/idbutton5"
app:layout_constraintTop_toBottomOf="@+id/idbutton4" />
</android.support.constraint.ConstraintLayout>