android - 减少 imageView 和 TextView 之间的距离

标签 android android-layout material-design android-button material-components-android

我在线性布局中以这种方式定义了 ImageView 和按钮:

  <ImageView
                                android:id="@+id/imageView2"

                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_margin="4dp"
                                android:layout_weight=".1"
                                android:src="@drawable/clock"/>

                        <Button
                                android:id="@+id/buttonEntryHour"
                                style="@style/Widget.AppCompat.Button.Borderless"
                                android:layout_width="0dp"
                                android:layout_height="wrap_content"
                                android:layout_margin="1dp"
                                android:layout_weight=".33"
                                android:onClick="clickTimePicker1"
                                android:text="@string/otrosEntryHourText"
                                android:textAlignment="textStart"
                                android:textColor="@color/colorPrimary"
                                app:layout_constraintEnd_toEndOf="parent"
                                app:layout_constraintStart_toStartOf="parent" />

但它们以不受欢迎的方式显示,如下所示:

enter image description here

我希望缩小图标和文本之间的距离,所以它显示如下:

enter image description here

如何实现这样的目标?

最佳答案

您布局中的问题是使用 android:layout_weight。删除它们。

但只需使用 MaterialButton使用 app:icon 属性,而不是 2 个 View :

    <com.google.android.material.button.MaterialButton
        style="@style/Widget.MaterialComponents.Button.Icon"
        app:icon="@drawable/ic_add_24px"
        android:text="Button"
        ../>

enter image description here

您可以使用 app:iconPadding 属性自定义图标和按钮文本之间的填充。

类似于:

    <com.google.android.material.button.MaterialButton
        style="@style/Widget.MaterialComponents.Button.Icon"
        app:icon="@drawable/ic_add_24px"
        app:iconPadding="xxdp"
        android:text="Button"
        ../>

您还可以使用 TextButton.Icon 样式:

    <com.google.android.material.button.MaterialButton
        style="@style/Widget.MaterialComponents.Button.TextButton.Icon"
        app:icon="@drawable/ic_add_24px"
        android:text="Button"
        ../>

enter image description here

OutlinedButton.Icon 样式:

<com.google.android.material.button.MaterialButton
    style="@style/Widget.MaterialComponents.Button.OutlinedButton.Icon"
    app:icon="@drawable/ic_add_24px"
    android:text="Button"
    ../>

enter image description here

关于android - 减少 imageView 和 TextView 之间的距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57851719/

相关文章:

angularjs - 如何使用 Angular Material md-grid-list

css - Angular MdSelect 没有下划线未定义

java - 将 List<String> 传递给 String... 参数

android - 在画廊 View 中显示来自特定文件夹的图像

java - 如何为新项目添加 RecyclerView 幻灯片动画

android-layout - Android 中带倒计时器的进度条

android - 设计带有圆角和内阴影的 EditText

android - 从另一个文件访问变量

java - Tango API 链接私有(private)原生库 API24+

android - 如何使用 Material 设计主题使按钮变圆?