【问题标题】:Arranging buttons with android: get rid of vertical space用android排列按钮:摆脱垂直空间
【发布时间】:2020-12-24 16:50:13
【问题描述】:

我是安卓新手。我主要了解 LaTex 的布局,但也了解 Swing、SWT 和 javafx 的外行水平。

作为练习,我尝试了一个非常简单的计算器布局,它基本上归结为拥有一组按钮。

无论我尝试什么,按钮行之间都保持垂直距离。

我已更改行的背景颜色,以查看不需要的空间属于谁。

截图:

代码(xml):

<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"
    tools:context=".MainActivity">

    <TableLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="1dp"
        app:layout_constraintTop_toTopOf="parent"
        tools:layout_editor_absoluteX="-28dp">

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="1dp"
            android:background="#00ff00">

            <ImageButton
                android:id="@+id/imageButton1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="0dp"
                android:layout_marginLeft="0dp"
                android:layout_marginTop="0dp"
                android:src="@drawable/img_a"
                tools:srcCompat="@tools:sample/avatars" />

            <ImageButton
                android:id="@+id/imageButton2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="0dp"
                android:layout_marginLeft="0dp"
                android:layout_marginTop="0dp"
                android:src="@drawable/img_a"
                tools:srcCompat="@tools:sample/avatars" />

            <ImageButton
                android:id="@+id/imageButton3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="0dp"
                android:layout_marginLeft="0dp"
                android:layout_marginTop="0dp"
                android:src="@drawable/img_a"
                tools:srcCompat="@tools:sample/avatars" />

            <ImageButton
                android:id="@+id/imageButton4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="0dp"
                android:layout_marginLeft="0dp"
                android:layout_marginTop="0dp"
                android:src="@drawable/img_a"
                tools:srcCompat="@tools:sample/avatars" />

            <ImageButton
                android:id="@+id/imageButton5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="0dp"
                android:layout_marginLeft="0dp"
                android:layout_marginTop="0dp"
                android:src="@drawable/img_a"
                tools:srcCompat="@tools:sample/avatars" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="1dp"
            android:background="@color/black">

            <ImageButton
                android:id="@+id/imageButton11"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="0dp"
                android:layout_marginLeft="0dp"
                android:layout_marginTop="0dp"
                android:src="@drawable/img_a"
                tools:srcCompat="@tools:sample/avatars" />

            <ImageButton
                android:id="@+id/imageButton12"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="0dp"
                android:layout_marginLeft="0dp"
                android:layout_marginTop="0dp"
                android:src="@drawable/img_a"
                tools:srcCompat="@tools:sample/avatars" />

            <ImageButton
                android:id="@+id/imageButton13"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="0dp"
                android:layout_marginLeft="0dp"
                android:layout_marginTop="0dp"
                android:src="@drawable/img_a"
                tools:srcCompat="@tools:sample/avatars" />

            <ImageButton
                android:id="@+id/imageButton14"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="0dp"
                android:layout_marginLeft="0dp"
                android:layout_marginTop="0dp"
                android:src="@drawable/img_a"
                tools:srcCompat="@tools:sample/avatars" />

            <ImageButton
                android:id="@+id/imageButton15"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="0dp"
                android:layout_marginLeft="0dp"
                android:layout_marginTop="0dp"
                android:src="@drawable/img_a"
                tools:srcCompat="@tools:sample/avatars" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </TableLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

我想去掉明显属于表格行的按钮上方和下方的绿色和黑色空间。

我该怎么做,除了那些神奇的词,我想了解什么和为什么出错了。

在使用表格布局之前,我尝试过垂直布局和嵌套水平布局,结果大致相同。

【问题讨论】:

    标签: android android-layout android-tablelayout android-imagebutton


    【解决方案1】:

    您只需要使用 android:padding="0dp" 将每个ImageButton 的填充设置为0,因为似乎ImageButton 默认样式具有此填充。

    <?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"
        tools:context=".MainActivity">
    
        <TableLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">
    
            <TableRow
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#00ff00">
    
                <ImageButton
                    android:id="@+id/imageButton1"
                    android:layout_width="wrap_content"
                    android:padding="0dp"
                    android:layout_height="wrap_content"
                    android:src="@drawable/img_a"
                    tools:srcCompat="@tools:sample/avatars" />
    
                <ImageButton
                    android:id="@+id/imageButton2"
                    android:layout_width="wrap_content"
                    android:padding="0dp"
                    android:layout_height="wrap_content"
                    android:src="@drawable/img_a"
                    tools:srcCompat="@tools:sample/avatars" />
    
                <ImageButton
                    android:id="@+id/imageButton3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="0dp"
                    android:src="@drawable/img_a"
                    tools:srcCompat="@tools:sample/avatars" />
    
                <ImageButton
                    android:id="@+id/imageButton4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="0dp"
                    android:src="@drawable/img_a"
                    tools:srcCompat="@tools:sample/avatars" />
    
                <ImageButton
                    android:id="@+id/imageButton5"
                    android:layout_width="wrap_content"
                    android:padding="0dp"
                    android:layout_height="wrap_content"
                    android:src="@drawable/img_a"
                    tools:srcCompat="@tools:sample/avatars" />
            </TableRow>
    
            <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/black">
    
                <ImageButton
                    android:id="@+id/imageButton11"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="0dp"
                    android:src="@drawable/img_a"
                    tools:srcCompat="@tools:sample/avatars" />
    
                <ImageButton
                    android:id="@+id/imageButton12"
                    android:layout_width="wrap_content"
                    android:padding="0dp"
                    android:layout_height="wrap_content"
                    android:src="@drawable/img_a"
                    tools:srcCompat="@tools:sample/avatars" />
    
                <ImageButton
                    android:id="@+id/imageButton13"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="0dp"
                    android:src="@drawable/img_a"
                    tools:srcCompat="@tools:sample/avatars" />
    
                <ImageButton
                    android:id="@+id/imageButton14"
                    android:layout_width="wrap_content"
                    android:padding="0dp"
                    android:layout_height="wrap_content"
                    android:src="@drawable/img_a"
                    tools:srcCompat="@tools:sample/avatars" />
    
                <ImageButton
                    android:id="@+id/imageButton15"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="0dp"
                    android:src="@drawable/img_a"
                    tools:srcCompat="@tools:sample/avatars" />
            </TableRow>
    
            <TableRow
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
    
            <TableRow
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
    
            <TableRow
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </TableLayout>
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    
    

    【讨论】:

    • 一旦你知道有这样的东西 "android:padding" ... 。非常感谢,这解决了我的问题。
    • @GyroGearloose 我找到了here 这是应用到ImageButton 的默认样式,默认填充是8dp 到4 个方向
    • @GyroGearloose 在here 中查找Base.Widget.AppCompat.ImageButton,这是默认的android 框架样式值...您将获得abc_btn_default_mtrl_shape for ImagreButton,可以找到here
    • 再次感谢,通过这个链接我还解决了我的下一个问题,子和上标不适用于默认按钮,并且所有希腊字母都转换为大写:有一个开关 android:textAllCaps="false"默认情况下为 true。
    猜你喜欢
    • 1970-01-01
    • 2018-04-01
    • 2015-02-05
    • 2021-10-25
    • 2012-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-03
    相关资源
    最近更新 更多