Floating action button doesn't appear in kitka

2019-07-17 04:33发布

问题:

I have created a single page app where the floating action button is used to share content from the page. It works flawlessly in android lollipop but when i ran the app on kitkat, it doesn't show. Since the only interactive component is the FAB, the user is left with nothing to do but stare. I ran some search and found that the FAB should work perfectly on kitkat and that there are some issues only when it comes to lollipop(like padding). But, it works just fine in lollipop. Do I have to change it to a button for versions older than lollipop? My first question here. Any help is appreciated. Thanks.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true"
    tools:context="com.ataraxianstudios.decksharer.MainActivity"
    >


    <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="@drawable/share1"/>

<ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#016cb4">
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/llMain"
        >
<Spinner
        android:id="@+id/osversions"
        android:layout_width="220dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        />
    <ImageView
        android:layout_width="60dp"
        android:layout_height="80dp"
        android:id="@+id/selectedImage"
        />

</LinearLayout>
    </ScrollView>
</android.support.design.widget.CoordinatorLayout>

回答1:

I also have the same problem may be it is due to material design as material design introduced after kitkat so there be some problem on some tabs or layouts but try this it works for me maybe works for you..

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout     
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:fitsSystemWindows="true"
tools:context="com.ataraxianstudios.decksharer.MainActivity"
>



//Replace your fab from here

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#016cb4">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:id="@+id/llMain"
    >
<Spinner
    android:id="@+id/osversions"
    android:layout_width="220dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    />
<ImageView
    android:layout_width="60dp"
    android:layout_height="80dp"
    android:id="@+id/selectedImage"
    />

</LinearLayout>
</ScrollView>


//to here:
<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="@drawable/share1"/>
</android.support.design.widget.CoordinatorLayout>


回答2:

Are you using design support library? This is an example:

<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_input_add" />

And in your build.gradle:

dependencies { ..... compile 'com.android.support:design:xx.yy.zz' }

Finally, in your Activity you can reference the FAB like:

FloatingActionButton btn = (FloatingActionButton) findViewById(R.id.fab);

Hope it helps you!!