android - drawableLeft/Right 等不适用于 Theme.MaterialComponents

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

当为 Activity Theme.MaterialComponents.... 使用主题时,如果我想将 drawableLeft/Right 等与按钮一起使用,它们不会显示。但是,如果我使用主题 Base.Theme.AppCompat,它们会按预期工作。 这是按钮:

       <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Test Button"
            android:drawableLeft="@drawable/ic_envelope"/>

这就是主题 Base.Theme.MaterialComponents 的样子 enter image description here

这是 Base.Theme.AppCompat enter image description here

如何让 drawableLeftBase.Theme.MaterialComponents 主题一起工作?

最佳答案

只需使用 MaterialButton使用 app:icon 属性:

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

Screenshot of an outlined button with an icon

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

Screenshot of a filled button with an icon

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

Screenshot of a borderless button with an icon

使用 app:iconGravity 来配置 start|end|textStart|textEnd

<com.google.android.material.button.MaterialButton
    app:iconGravity="end"
    ../>

enter image description here

关于android - drawableLeft/Right 等不适用于 Theme.MaterialComponents,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58083679/

相关文章:

android - 如何在android中使用通用图像加载器缓存图像

css - Material 设计对网络的有意义的转变

android - 按钮缩放到背景图像而不是内容

java - onTouch 方法 - 跟踪已单击或按下的按钮实例

java - USBFS IOCTL 调用 witn JNA 在 64 位架构中不起作用

Android webview 允许使用当前的 Android Studio 访问相机

android - 非抽象且不重写抽象方法

android - 是否可以向 MaterialDateRange Picker 添加页脚?

material-design - 从 angularjs 初始化 materializecss 下拉菜单时出错

android - 未知属性 android :elevation