按钮不会填满整个空间

时间:2019-01-21 19:51:13

标签: android-studio android-scrollview android-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

如您所见,按钮周围有我所不需要的空间。有什么方法可以使按钮充满整个空间吗?

1 个答案:

答案 0 :(得分:1)

尝试使用下面的代码(在此示例中,我删除了android:drawableTop =“ @ drawable / default_icon”,但只需再次添加即可)。

 -- control output of test results
Declare @filepath Varchar(200)
set @filepath='C:\Users\XXXXXX\Desktop\Automation Rewrite\Enrollment\Test Results\'
declare @filename varchar(100)

set @filename = 'enrollment_OH_MD_1_' + CONVERT(varchar(12),getdate()) + '.txt'
select @filename
declare @fullpath Varchar(200)
set @fullpath = '"'+ltrim(@filepath+@filename)+'"'
select @fullpath
:out @fullpath