drawableStart 不显示drawable

问题描述 投票:0回答:3

我正在学习android,书中的挑战之一说要自己添加上一个按钮。我照做了,并请朋友帮忙把箭头放在左侧,他照做了,但他不明白为什么

drawableStart
不起作用。 (
drawableLeft
成功了)

有人知道为什么

drawableStart
不起作用吗?或者更好的是:如何修复它?

    <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <Button
        android:id="@+id/previous_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawableStart="@drawable/arrow_left"
        android:text="@string/previous_button"
        android:drawablePadding="4dp"/>

    <Button
    android:id="@+id/next_button"
    android:layout_width="wrap_content"
    android:gravity="center"
    android:layout_height="wrap_content"
    android:drawableEnd="@drawable/arrow_right"
    android:drawablePadding="4dp"
    android:text="@string/next_button"/>
</LinearLayout>

themes.xml下的代码:

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.GeoQuiz" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/purple_500</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
        <!-- Customize your theme here. -->
    </style>
</resources>
android android-layout
3个回答
3
投票

您似乎正在使用 Material design 按钮,因此请使用

android:icon
而不是
android:drawableStart

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <Button
        android:id="@+id/previous_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:icon="@drawable/arrow_left"
        android:text="@string/previous_button"
        android:drawablePadding="4dp" />
    <Button
        android:id="@+id/next_button"
        android:layout_width="wrap_content"
        android:gravity="center"
        android:layout_height="wrap_content"
        android:icon="@drawable/arrow_right"
        android:drawablePadding="4dp"
        android:text="@string/next_button"/>
</LinearLayout>

更新:

没用。现在它不显示任何一个箭头。并且该程序不再在模拟器中运行

似乎是与

android
指令的兼容性问题,因此,将其更改为
app
指令,并在使用时添加
Widget.MaterialComponents.Button.TextButton.Dialog
样式

<LinearLayout android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <Button
        android:id="@+id/previous_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:icon="@drawable/arrow_left"
        android:text="@string/previous_button"
        style="@style/Widget.MaterialComponents.Button.TextButton.Dialog"
        android:drawablePadding="4dp" />

    <Button
        android:id="@+id/next_button"
        android:layout_width="wrap_content"
        android:gravity="center"
        android:layout_height="wrap_content"
        app:icon="@drawable/arrow_right"
        android:drawablePadding="4dp"
        android:text="@string/next_button"/>
    style="@style/Widget.MaterialComponents.Button.TextButton.Dialog"
</LinearLayout>

1
投票

经过大量搜索,我终于找到了如何让它发挥作用 android:drawableStart 不起作用似乎是一个问题,这里是问题和修复的链接 https://github.com/material-components/material-components-android/issues/1174

这是修复方法

<Button
    android:id="@+id/previous_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:icon="@drawable/arrow_left"
    app:iconGravity="textStart"
    app:iconTint="@android:color/white"
    android:text="@string/previous_button"
    android:drawablePadding="4dp"/>

不要使用drawableBottom|Top|Left|...,而是使用MaterialButton的属性app:icon、app:iconGravity和app:iconTint

注意,drawableStart 似乎是唯一一个存在其他问题的问题

<com.google.android.material.button.MaterialButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="12dp"
    android:text="@string/your_text"
    android:drawableEnd="@drawable/your_image"/>

<com.google.android.material.button.MaterialButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="12dp"
    android:text="@string/your_text"
    android:drawableTop="@drawable/your_image"/>

<com.google.android.material.button.MaterialButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="12dp"
    android:text="@string/your_text"
    android:drawableBottom="@drawable/your_image"/>

然后向左

<com.google.android.material.button.MaterialButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="12dp"
    android:text="@string/your_text"
    app:icon="@drawable/arrow_left"
    app:iconGravity="textStart"
    app:iconTint="@android:color/white"/>

0
投票

从这里的答案来看,这似乎是最简单的事情。
我在设置中将其修改为阿拉伯语并确认它可以正常工作。

<Button
    app:icon="@drawable/baseline_add_24_white"
    app:iconGravity="textStart"
/>
© www.soinside.com 2019 - 2024. All rights reserved.