【问题标题】:Move Floating Action Button above keyboard在键盘上方移动浮动操作按钮
【发布时间】:2023-04-07 04:03:01
【问题描述】:

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

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

编辑:

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>

【问题讨论】:

  • 我认为是你的布局问题。
  • 我会发布我的activity_main
  • 您是否添加了@Raj 答案。试试他的方法
  • @IntelliJAmiya 是的,我做到了。它所做的只是在我打开应用程序时打开键盘。

标签: android android-studio android-softkeyboard floating-action-button


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    我有一个类似的问题,遵循@MrPlow 的建议,但是他在原始的RelativeLayout 中添加了一个Scrollview,然后在ScrollView 中添加了另一个RelativeLayout(以保留格式结构),我只是将整个布局包装在一个ScrollView 和它'刚刚工作'。 OP 的最终结果将是:

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

    【讨论】:

      【解决方案3】:

      试试这个:

      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

      【讨论】:

        【解决方案4】:

        真正做到这一点。它为我工作。

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

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

        【讨论】:

        • 根本没有帮助我:(
        • 您是否希望将其显示在键盘顶部或稍微向上移动以使其可见?如果你在键盘上显示它,键盘就无法按下。
        • 我想把它显示在键盘上,这样它就可以看到了。
        • 如果你想要键盘后面的fab按钮,请这样做,谢谢
        【解决方案5】:

        你现在的布局是这样的:

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

        您需要在 RelativeLayout 标签内添加一个滚动视图。此外,滚动视图不喜欢有多个直接子视图,因此您需要在滚动视图内部使用一个 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 仍然在键盘后面 :(
        • 如果有帮助,请发布我的 activity_main
        • 看起来您需要做的就是将此修复应用于您的 activity_main 布局,而不是其他任何东西。将滚动视图放在整个页面周围,除了最外面的视图组。
        【解决方案6】:

        这样做

        <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 仍在键盘后面。
        • 发布了我的activity_main.xml
        【解决方案7】:

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

        【讨论】:

        • 我该怎么做?我会发布我的布局,你可以告诉我怎么做。
        猜你喜欢
        • 2016-03-15
        • 1970-01-01
        • 2017-03-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-06-07
        • 2015-06-25
        • 2016-12-08
        相关资源
        最近更新 更多