将浮动操作按钮移到键盘上方

5

我有一个Floating Action Button (GitHub链接),当我打开(软件)键盘时,浮动操作按钮会隐藏在键盘后面。

有没有办法将它推到键盘上方?

编辑:

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>

我认为这是你的布局问题。 - IntelliJ Amiya
1
我会发布我的activity_main。 - Gursimran Bedi
你有加上 @Raj 的回答吗?试试他的方法。 - IntelliJ Amiya
@IntelliJAmiya 是的,我试过了。只是在我打开应用程序时打开了键盘。 - Gursimran Bedi
7个回答

1
你的布局现在是这样的:

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

您需要在RelativeLayout标签内添加一个ScrollView。此外,ScrollView不喜欢有多个直接子视图,因此您需要在ScrollView内部添加一个RelativeLayout。因此,您的布局最终将如下所示:

<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.)

FAB仍然落后于键盘 :( - Gursimran Bedi
如果有帮助的话,我已经发布了我的activity_main。 - Gursimran Bedi
看起来你只需要将这个修复应用到你的activity_main布局上,而不是其他什么东西。将ScrollView放在整个页面周围,除了最外层的ViewGroup。 - MrPlow

1

我遇到了类似的问题,按照 @MrPlow 的建议操作,但他在原始的RelativeLayout中添加了一个Scrollview,然后在ScrollView内部再添加了另一个RelativeLayout(以保留格式结构),而我只是将整个布局包装在一个ScrollView中,它就“奏效了”。最终结果如下:

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

0

试试这个:

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>

我所做的就是将fab放在ScrollView中。


0
一个解决方案是将整个布局放在ScrollView中。

我该怎么做?我会发布我的布局,您可以告诉我如何做。 - Gursimran Bedi

0
无论FAB是否移动到键盘上方,取决于清单文件和特定的"android:windowSoftInputMode"标签。
为了使其在软键盘上方移动,这是我使用的代码(清单文件):
<activity 
    android:name=".activities.YourActivity" 
    android:screenOrientation="portrait" 
    android:windowSoftInputMode="adjustPan" 
    android:theme="@style/AppTheme.NoActionBar" />

为了让它停留在软键盘后面,请使用以下代码:

<activity 
    android:name=".activities.YourActivity" 
    android:screenOrientation="portrait" 
    android:theme="@style/AppTheme.NoActionBar" />

-1

做这个

<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>

由于您的项目现在位于滚动视图中,它们将会滚动。


当我输入那段代码时,FAB仍然在键盘后面。 - Gursimran Bedi
发布了我的 activity_main.xml。 - Gursimran Bedi

-1

试着这样做。这对我有用。

在您的清单文件中将stateVisible|adjustPan添加到您的活动中。

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

完全没有帮助 :( - Gursimran Bedi
你想把它显示在键盘的顶部还是稍微上移一点,以便它可以被看到?如果你把它显示在键盘的顶部,那么键盘就无法按下。 - RajSharma
我想把它显示在键盘的顶部,这样它就能被看到。 - Gursimran Bedi
如果您想要在键盘后面放置Fab按钮,请执行此操作,谢谢。 - vuhung3990

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