Move Floating Action Button above keyboard

2019-01-28 07:26发布

问题:

I have this Floating Action Button (GitHub link) and when I open a (software) keyboard the Floating Action Button hides behind the keyboard.

Is there a way that I can push it above the keyboard?

Edit:

tab_random.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <View
        android:layout_width="match_parent"
        android:layout_height="156dp"
        android:background="@color/primary_color"
        />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/from"
        android:inputType="number"
        android:imeOptions="actionNext"
        android:layout_centerHorizontal="true"
        android:hint="From"
        android:paddingStart="5dp"
        android:textColor="#FFF"
        android:fontFamily="sans-serif-light"
        android:textSize="44sp"
        android:backgroundTint="@color/fab_ripple"
        android:layout_marginStart="10dp"
            />

    <EditText
        android:textSize="44sp"
        android:layout_marginStart="10dp"
        android:paddingStart="5dp"
        android:fontFamily="sans-serif-light"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/from"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="To"
        android:inputType="number"
        android:imeOptions="actionSend"
/>
</RelativeLayout>

activity_main.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:fab="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="activity.MainActivity">

    <include
        android:id="@+id/toolbar"
        layout="@layout/tool_bar" />

    <slidingModel.SlidingTabLayout
        android:layout_below="@+id/toolbar"
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:elevation="4dp"
        android:background="@color/primary_color"
        />

    <android.support.v4.view.ViewPager
        android:layout_below="@id/tabs"
        android:id="@+id/pager"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"

        />

    <com.melnykov.fab.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="16dp"
        fab:fab_colorNormal="@color/fab_normal"
        fab:fab_colorPressed="@color/fab_pressed"
        fab:fab_colorRipple="@color/fab_ripple"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true" />

</RelativeLayout>

回答1:

Your layout is like this right now:

<Relative Layout>
    <View/>
    <EditText/>
    <other stuff.../>
</RelativeLayout>

You need to add a scrollview inside the RelativeLayout tags. Also, scrollviews don't like having more than one direct child view, so you'll need a Relativelayout inside of the scrollview. So, your layout will wind up looking like this:

<RelativeLayout> (this was already here)
    <ScrollView> (this is new)
        <RelativeLayout> (this is also new)
            <View, EditText, Etc. in here/> (this is what used to be inside that original relative layout.)
        </RelativeLayout> (closing up the new Relativelayout)
    </ScrollView> (closing the new Scrollview)
</RelativeLayout> (closing the original Relative Layout.)


回答2:

TRU TO DO THIS. iT WORKED FOR ME.

add stateVisible|adjustPan TO YOUR ACTIVTY IN YOUR MANIFEST FILE.

<activity 
android:name="com.comapny.applicationname.activityname"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateVisible|adjustPan"/>


回答3:

I had a similar problem, followed suggestion by @MrPlow, but where he added a Scrollview inside the original RelativeLayout and then added another RelativeLayout inside of the ScrollView (to preserve the formatting structure), I just wrapped the entire layout in a ScrollView and it 'just worked'. Final for OP would then be:

<Scrollview>
    <RelativeLayout>
        <View/>
        <EditText/>
        <other stuff.../>
    </RelativeLayout>
</Scrollview>


回答4:

A solution can be to put your whole layout inside a ScrollView.



回答5:

Do this

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ScrollView 
android:layout_width="match_parent"
android:layout_height="match_parent">

<View
    android:layout_width="match_parent"
    android:layout_height="156dp"
    android:background="@color/primary_color"
    />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/from"
    android:inputType="number"
    android:imeOptions="actionNext"
    android:layout_centerHorizontal="true"
    android:hint="From"
    android:paddingStart="5dp"
    android:textColor="#FFF"
    android:fontFamily="sans-serif-light"
    android:textSize="44sp"
    android:backgroundTint="@color/fab_ripple"
    android:layout_marginStart="10dp"
        />

<EditText
    android:textSize="44sp"
    android:layout_marginStart="10dp"
    android:paddingStart="5dp"
    android:fontFamily="sans-serif-light"
    android:layout_centerHorizontal="true"
    android:layout_below="@+id/from"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:hint="To"
    android:inputType="number"
    android:imeOptions="actionSend"
  />
  </ScrollView>
  </RelativeLayout>

Since your items are now inside scrollview they will scroll.



回答6:

Try with this:

activity_main.xml

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="activity.MainActivity">

<include
    android:id="@+id/toolbar"
    layout="@layout/tool_bar" />

<slidingModel.SlidingTabLayout
    android:layout_below="@+id/toolbar"
    android:id="@+id/tabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:elevation="4dp"
    android:background="@color/primary_color"
    />

<android.support.v4.view.ViewPager
    android:layout_below="@id/tabs"
    android:id="@+id/pager"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"

    />

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

</ScrollView>

All I have done is put the fab inside a ScrollView.