我正在努力增加左边的可绘制图标和右边的文本之间的差距。我正在使用drawablePadding。但这似乎没有任何效果。这是密码-
<?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"
android:background="@drawable/background_gradient"
tools:context=".Authentication">
<Button
android:id="@+id/google_sign_in_btn"
android:drawableLeft="@drawable/g_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="@color/white"
android:fontFamily="@font/roboto_medium"
android:textColor="#8A000000"
android:text="@string/sign_in_with_google_txt"
android:padding="8dp"
android:drawablePadding="100dp"
android:textAllCaps="false"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
结果是-
发布于 2021-01-26 17:42:38
drawablePadding不会在materialButtons上工作,因为materialButton类读取来自app:iconPadding
属性的可绘制填充,而不是drawablePadding
。
如果您查看“材料”按钮的源代码,您将看到以下内容:
iconPadding = attributes.getDimensionPixelSize(R.styleable.MaterialButton_iconPadding, 0);
在这里,您可以看到图标的填充是从iconPadding属性读取的。将其与TextView (Button扩展textView)进行比较,我们有:
for (int i = 0; i < n; i++) {
int attr = a.getIndex(i);
switch (attr) {
//lots of cases
case com.android.internal.R.styleable.TextView_drawablePadding:
drawablePadding = a.getDimensionPixelSize(attr, drawablePadding);
break;
}
}
要了解这些xml属性是如何工作的,请参阅创建视图类
发布于 2022-12-02 19:05:40
我也有同样的问题,我把按钮换成了https://developer.android.com/reference/com/google/android/material/button/MaterialButton
就像这样,您可以在Material中使用app:iconPadding属性,而不是使用android:drawablePadding,来增加或减少可绘制和文本之间的填充。这对我来说很好。您甚至可以设置负值,如app:iconPadding="-5",这会在可绘制和文本之间产生更小的填充。
例如:
<com.google.android.material.button.MaterialButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/your_drawable"
android:text="Your Text"
app:iconPadding="10dp"/>
我希望这能帮到你!
https://stackoverflow.com/questions/65895952
复制相似问题