【问题标题】:How do I display icon on FloatingActionButton?如何在 FloatingActionButton 上显示图标?
【发布时间】:2018-08-29 03:50:45
【问题描述】:

我在 FloatingActionButton 上显示图标时遇到问题。图标失去透明度。我有 compileSdkVersion 28.

依赖项是:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    //noinspection GradleCompatible
    implementation 'com.android.support:appcompat-v7:28.0.0-alpha1'
    //noinspection GradleDynamicVersion
    implementation 'com.android.support:recyclerview-v7:28.0.0-rc02'
    implementation 'com.android.support:design:28.+'
    implementation 'com.android.support:cardview-v7:28.0.0-rc01'
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso- 
    core:3.0.2'
    implementation 'com.squareup.picasso:picasso:2.71828'

}

布局是:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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="@color/colorDivider"
tools:context=".activities.MainActivity">

<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerViewId"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="vertical" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fabAddCity"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="16dp"
    android:clickable="true"
    android:src="@mipmap/ic_plus"
    app:backgroundTint="@color/colorPrimaryDark"
    app:borderWidth="0dp"
    android:focusable="true"/>
</FrameLayout>

它看起来像这样:

【问题讨论】:

    标签: android floating-action-button


    【解决方案1】:

    最近我遇到了使用问题 28.0.0-** 库的

    所以我建议你

    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support:design:27.1.1'
    

    然后改变

     compileSdkVersion 27
     targetSdkVersion 27
    

    浮动按钮示例

    <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_margin="@dimen/fab_margin"
            android:src="@android:drawable/ic_dialog_email" />
    

    添加 Dimesn

    dimens.xml
    <resources>
        <!-- Default screen margins, per the Android Design guidelines. -->
        <dimen name="activity_horizontal_margin">16dp</dimen>
        <dimen name="activity_vertical_margin">16dp</dimen>
        <dimen name="fab_margin">16dp</dimen>
    </resources>
    

    我建议再次查看示例以获得更多许可 https://www.androidhive.info/2015/12/android-material-design-floating-action-button/

    【讨论】:

    • 嘿 ashvin,你能确认这是否是设计库问题,即使我面临与 fab 类似的问题
    • @Neji 先生,我不确定这个问题是否由谷歌解决,但我们通过降级库版本来解决这个问题
    • 我做了同样的事情,它工作正常,你是使用 svg 文件作为图标还是 png
    • 我正在使用 png 先生
    • @Neji 这可能会更有帮助stackoverflow.com/questions/50940219/…
    【解决方案2】:

    问题:

    app:backgroundTint="@color/colorPrimaryDark"

    它使透明部分具有您选择的背景颜色。

    所以你可以用

    替换它

    android:tint="@android:color/colorPrimaryDark"

    android:tint="@android:color/background_light"(如果你想要白色)

    或者你可以用这个替换你的代码:

    <android.support.design.widget.FloatingActionButton
    android:id="@+id/fabAddCity"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="16dp"
    android:clickable="true"
    android:tint="@android:color/background_light"
    app:srcCompat="@android:drawable/ic_input_add"
    app:borderWidth="0dp"
    android:focusable="true"/>
    

    我意识到你正在使用 colorPrimaryDark 作为你的色调,如果你坚持使用,请将其更改为

    android:tint="@android:color/colorPrimaryDark"

    【讨论】:

      【解决方案3】:

      第一个可绘制对象应位于正确的可绘制文件夹中,而不是 mipmap 文件夹中。其次,看起来您正在使用的图像正在显示,但可能不是您想要的颜色。如果是这种情况,您可以使用 android:tint="@color/section_arrowup_color" 在 xml 中对其进行着色,但这可能无法正常工作,具体取决于您的图像格式。您也可以通过编程方式进行着色,但这可能具有相同的效果。最后,您可以创建一个新的图像资产,它是您想要的颜色。由于它是一个加号,您可以轻松地创建一个矢量可绘制对象并在 xml 中设置颜色

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-05-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多