Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/209.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何将MaterialButton文本旁边的图标与已定义的填充对齐?_Android_Material Design - Fatal编程技术网

Android 如何将MaterialButton文本旁边的图标与已定义的填充对齐?

Android 如何将MaterialButton文本旁边的图标与已定义的填充对齐?,android,material-design,Android,Material Design,您好,我在Android上对齐材质按钮中的图标时遇到问题。我有一个按钮,在文本旁边有宽度、文本和图标。我想将图标8dp左对齐到文本,我尝试通过com.google.android.material.button.MaterialButton应用程序:iconGravity来实现。但结果并不像预期的那样 正如您所看到的,随着文本越来越长,图标填充越来越小。我还尝试了defineapp:iconadding(默认值为8dp),但结果相同 这是虫子还是我做错了 布局: <?xml versio

您好,我在Android上对齐材质按钮中的图标时遇到问题。我有一个按钮,在文本旁边有宽度、文本和图标。我想将图标
8dp
左对齐到文本,我尝试通过
com.google.android.material.button.MaterialButton
应用程序:iconGravity
来实现。但结果并不像预期的那样

正如您所看到的,随着文本越来越长,图标填充越来越小。我还尝试了define
app:iconadding
(默认值为
8dp
),但结果相同

这是虫子还是我做错了

布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    android:padding="16dp">

    <com.google.android.material.button.MaterialButton
        style="@style/MaterialButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:icon="@drawable/ic_launcher"
        app:iconGravity="textStart"
        tools:text="L" />

    <com.google.android.material.button.MaterialButton
        style="@style/MaterialButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:icon="@drawable/ic_launcher"
        app:iconGravity="textStart"
        tools:text="Lorem ipsum" />

    <com.google.android.material.button.MaterialButton
        style="@style/MaterialButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:icon="@drawable/ic_launcher"
        app:iconGravity="textStart"
        tools:text="Lorem ipsum dolor sit amet" />
</LinearLayout>

风格:

<style name="MaterialButton" parent="Widget.MaterialComponents.Button">
    <item name="android:textColor">@color/white</item>
    <item name="android:textAppearance">@style/TextTitleAllCapsBold</item>
    <item name="android:fontFamily">@font/roboto_condensed_regular</item>
    <item name="android:padding">@dimen/padding_medium</item>
    <item name="backgroundTint">@color/colorPrimary</item>
    <item name="iconSize">32dp</item>
</style>

@颜色/白色
@样式/文本标题AllCapsbold
@font/roboto\u浓缩\u常规
@二聚体/填充物/中等
@颜色/原色
32dp

使用
implementation'com.google.android.material:material:1.0.0'
删除
app:iconGravity=“textStart”
删除
app:iconGravity=“textStart”

TL;博士

使用iconGravity=“textStart”指定图标时,按钮仅使用大写文本和/或单空格字体,并使用textAlignment=“center”(默认值)

我找到了修复这个bug的原因和解决方法。 由于属性
android:textAllCaps
,它严重地计算了图标的左填充

如果您有相同的经验,请删除(或设置为
false
)属性
android:textAllCaps
,并将其替换为strings.xml中的大写文本,或者在Java或Kotlin中使用
.toUpperCase()

这是一个在2018年9月发现的问题,到了10月,它被标记为固定在谷歌的网站上。但我正在使用最新的稳定版本的库,仍然有缺陷。这是我的

更新

该错误现在已在版本1.1.0-alpha02中修复

TL;博士

使用iconGravity=“textStart”指定图标时,按钮仅使用大写文本和/或单空格字体,并使用textAlignment=“center”(默认值)

我找到了修复这个bug的原因和解决方法。 由于属性
android:textAllCaps
,它严重地计算了图标的左填充

如果您有相同的经验,请删除(或设置为
false
)属性
android:textAllCaps
,并将其替换为strings.xml中的大写文本,或者在Java或Kotlin中使用
.toUpperCase()

这是一个在2018年9月发现的问题,到了10月,它被标记为固定在谷歌的网站上。但我正在使用最新的稳定版本的库,仍然有缺陷。这是我的

更新


该错误现已在版本1.1.0-alpha02

中修复,但不起作用。我想让图标紧靠文本,而不是左对齐。不起作用。我想有一个图标旁边的文字,而不是左对齐。