簡體   English   中英

將浮動操作按鈕移動到鍵盤上方

[英]Move Floating Action Button above keyboard

我有這個浮動操作按鈕(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>

活動_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>

你的布局現在是這樣的:

<Relative Layout>
    <View/>
    <EditText/>
    <other stuff.../>
</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.)

我有一個類似的問題,遵循@MrPlow 的建議,但是他在原始的相對布局中添加了一個 Scrollview,然后在 ScrollView 中添加了另一個相對布局(以保留格式結構),我只是將整個布局包裝在一個 ScrollView 中'剛剛工作'。 OP的最終結果將是:

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

一種解決方案是將整個布局放在 ScrollView 中。

試試這個:

活動_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

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

做這個

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

由於您的項目現在位於滾動視圖內,因此它們將滾動。

真正做到這一點。 它對我有用。

將 stateVisible|adjustPan 添加到您的清單文件中的活動中。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM