drawableStart不显示drawable

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>

请将XML布局代码以文本形式发布,而不是图片。 - Zain
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>

对不起,我刚开始学习安卓,我不知道那是什么意思。它应该在同一个文件(activity_main.xml)中吗?顶部的内容(看起来像是导入的库)只有:xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" 我应该在那里添加 Theme.MaterialComponents 吗? - Doragon
没问题,在左侧窗格中,有一个文件在res\values\styles.xml下面,你能分享一下它的内容吗? - Zain
不存在。在res/values下有:colors.xml、strings.xml和themes文件夹,其中包含2个themes.xml文件(一个带有括号中的“night”),我应该添加一个styles文件吗? - Doragon
看起来是themes.xml,名字不重要,但通常是styles.xml..请分享一下 - Zain
@Doragon,请查看答案中的更新部分。 - Zain
显示剩余3条评论

2
经过大量搜索,我终于弄清楚了如何使它工作。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"
/>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接