安卓:浮动操作按钮出现透明背景

4

我已经在我的应用程序中添加了FloatingActionButton,并使用png图标ic_send。当我使用gingerbread设备进行测试时,它一直显示没有背景色的按钮,但在lollipop上显示正常。

这是我的Activity代码:

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

import butterknife.Bind;
import butterknife.ButterKnife;

public class MainActivity2 extends AppCompatActivity {
    @Bind(R.id.toolbar)Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        ButterKnife.bind(this);
        setSupportActionBar(toolbar);

    }
}

以下是我的XML文件

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <include
        layout="@layout/toolbar" />

    <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/dp10"
        android:background="@color/card"
        android:gravity="center"
        card_view:cardCornerRadius="5dp">

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical"
            android:padding="@dimen/dp5">

            <EditText
                android:id="@+id/main_edt_from"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="@dimen/dp30"
                android:hint="@string/amount"
                android:inputType="numberDecimal" />
        </LinearLayout>

    </android.support.v7.widget.CardView>
</LinearLayout>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/btn_next"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/dp15"
    android:src="@drawable/ic_send"
    app:elevation="2dp"
    app:fabSize="normal"
    app:layout_anchor="@id/card"
    app:layout_anchorGravity="bottom|right|end" />
</android.support.design.widget.CoordinatorLayout>

我的 build.gradle 配置如下:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    defaultConfig {
        applicationId "com.kr.currencyconverter"
        minSdkVersion 10
        targetSdkVersion 23
        versionCode 4
        versionName "2.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.jakewharton:butterknife:7.0.1'
    compile 'com.android.support:appcompat-v7:23.0.1'
    compile 'com.android.support:support-v4:23.0.1'
    compile 'com.android.support:design:23.0.1'
    compile 'com.google.android.gms:play-services:8.1.0'
    compile 'com.android.support:cardview-v7:23.0.1'
}

我尝试的方法是将 app:backgroundTint="@color/accent" 添加到 FloatingActionButton 中,但它没有起作用。
这里有一张图片可以说明我的意思。在测试中,我得到了一个透明的按钮。 enter image description here

@dex 我添加了 app:backgroundTint="@color/accent" 但它没有起作用。 - Khalil Rumman
@Jolson,它没有起作用。 - Khalil Rumman
@KhalilRumman 尝试一些其他颜色,是否可见?并且请将颜色添加到浮动XML中。 - dex
@dex 实际上,它没有起作用。但我所做的是将目标SDK更改为22而不是23,并且我还更改了依赖项。现在它可以工作了, 因此我认为在支持棉花糖的设计库中应该有新的属性可用。 - Khalil Rumman
请访问 https://code.google.com/p/android/issues/list,然后点击“新问题”并提交错误报告。 - dex
显示剩余5条评论
4个回答

0

在FloatingActionButton上移除app:elevation="2dp"

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/btn_next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/dp15"
        android:src="@drawable/ic_send"
        //app:elevation="2dp"
        app:fabSize="normal"
        app:layout_anchor="@id/card"
        app:layout_anchorGravity="bottom|right|end" />

相反,使用给定的代码片段

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/btn_next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/dp15"
        android:src="@drawable/ic_send"
        app:fabSize="normal"
        app:layout_anchor="@id/card"
        app:layout_anchorGravity="bottom|right|end" />


感谢您发布代码,但是您能否在帖子中添加一些说明,解释为什么以及如何工作? - Rob Streeting
高度是我们在布局上设置图层的方式。FloatingButtonAction 的默认高度值为 6dp。因此,如果您将高度设置为 2dp,则背景将出现在布局的背景上。FloatingButtonAction 的最小高度为 6dp,以显示在布局的顶部图层上。如果我有错误,请纠正我。 - kofenko

0

你必须设置

app:backgroundTint="@color/yourcolor"

我认为“Tint”不能适用于低于“棒棒糖”的版本。 - Kishan Subhash

0

将此图标附加在这里

"@dimen/dp15"

这张图片似乎存在背景颜色。


你能解释一下吗?我已经做过了 android:layout_margin="@dimen/dp15" android:src="@drawable/ic_send" - Khalil Rumman
我的意思是,检查android:src="@drawable/ic_send"图像是否有背景颜色。 - Karthik Kumar
你可以尝试将ic_send设置为按钮背景吗? - Karthik Kumar
1
实际上这是没有背景的PNG格式。我向Google报告了此问题,发现这是支持库23.0.1中的一个错误,现在应该已经在23.1版本中修复了。 - Khalil Rumman
好的,知道了。谢谢。 - Karthik Kumar

0

我曾经遇到过与FloatingActionButton(FAB)类似的问题。在我的情况下,我正在Activity中使用fragments,并且在我的activity的布局文件中有FAB。因此,fragment会渲染在FAB的上方。所以我不得不在我的activity的onPostResume()方法中添加fab.bringToFront();来使它正常工作。

@Override
protected void onPostResume() {
    super.onPostResume();
    fab.bringToFront(); // fab - reference of FloatingActionButton
}

同时,检查布局的高度应该小于浮动操作按钮的高度。

这是支持库23.0.1中的一个错误,已经由Google修复,现在它可以正常工作了。谢谢 :) - Khalil Rumman

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