在Android键盘中调整平移问题

时间:2017-11-10 06:33:56

标签: java android keyboard adjustpan

所以基本上我想把底部按钮推到键盘顶部,然后在打开键盘后再按下其他内容。 问题是如果我使用调整平移按钮与编辑文本重叠,如果我使用调整调整大小按钮将不会出现。

<LinearLayout
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:background="@color/white"
android:orientation="vertical"
tools:context="com.lifeincontrol.activities.home.AddCompanionActivity"
>
<android.support.v7.widget.Toolbar
  android:id="@+id/toolbar"
  android:layout_width="match_parent"
  android:layout_height="@dimen/toolbar_height"
  android:background="?attr/colorPrimary"
  android:titleTextColor="@android:color/white"
  app:popupTheme="@style/AppTheme.PopupOverlay"
  >
</android.support.v7.widget.Toolbar>
<ScrollView
  android:id="@+id/scroll_view"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:fillViewport="true"
  >
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
  <ImageView
      android:id="@+id/companion_image"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_marginTop="40dp"
      android:src="@drawable/ic_add_companion_illustration"
      />
  <LinearLayout
      android:id="@+id/number_field"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_below="@+id/companion_image"
      android:layout_marginLeft="15dp"
      android:layout_marginTop="62dp"
      android:orientation="horizontal"
      >
    <LinearLayout
        android:id="@+id/country_code_layout_before"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:layout_marginBottom="7dp"
        android:orientation="vertical"
        >
      <LinearLayout
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:orientation="horizontal"
          >
        <ImageView
            android:id="@+id/text_country_code_before"
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:layout_marginRight="12dp"
            android:drawableRight="@drawable/arrow_drop_down"
            android:src="@drawable/flag_in"
            />
        <ImageView
            android:id="@+id/drop_down_before"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingRight="12dp"
            android:paddingTop="12dp"
            android:src="@drawable/down_arrow_logging"
            />
      </LinearLayout>
      <View
          android:layout_width="match_parent"
          android:layout_height="1dp"
          android:layout_marginRight="12dp"
          android:layout_marginTop="4dp"
          android:background="#5E000000"
          />
    </LinearLayout>
    <EditText
        android:id="@+id/number_edit_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:hint="Phone Number"
        android:inputType="number"
        android:maxLines="1"
        android:textColorHint="#aaa"
       />
    <ImageView
        android:id="@+id/phone_book"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginRight="11dp"
        android:src="@drawable/circle"
        android:visibility="gone"
        />
  </LinearLayout>
  <EditText
      android:id="@+id/name_edit_text"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_below="@+id/number_field"
      android:layout_marginLeft="16dp"
      android:layout_marginRight="16dp"
      android:layout_marginTop="30dp"
      android:hint="Name"
      android:imeActionLabel="Done"
      android:imeOptions="actionDone"
      android:maxLines="1"
      android:singleLine="true"
      android:textColorHint="#aaa"
      />
  <View
      android:layout_width="match_parent"
      android:layout_height="55dp"
      android:layout_below="@+id/name_edit_text"
      />
  <Button
      android:id="@+id/button_send"
      android:layout_width="match_parent"
      android:layout_height="55dp"
      android:layout_alignParentBottom="true"
      android:background="@color/companion_button_send"
      android:text="Send"
      android:textColor="@color/white"
      android:textSize="14sp"
      />
</RelativeLayout>
</ScrollView>
</LinearLayout>

first image before keyboard open

second image here i want to show the green button on top of keyboard

2 个答案:

答案 0 :(得分:0)

将以下行添加到您的活动java文件

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

然后将ScrollView添加到布局中。请记住,ScrollView只能容纳1个子视图。

<ScrollView
    android:id="@+id/scrollview"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

    <--------Your content---------->

  </RelativeLayout>

  </ScrollView>

答案 1 :(得分:0)

1.在您的清单文件中更改adjustpan以adjustResize,如下所示

<activity android:name=".MainActivity"
            android:windowSoftInputMode="adjustResize|stateHidden"/>
  1. 将您的xml代码放在scrollView

    <ScrollView
     android:layout_width="match_parent"
     android:layout_height="match_parent">
    
    <RelativeLayout
     android:layout_width="match_parent"
     android:layout_height="match_parent">
    <--------Your content---------->
    
    </RelativeLayout>
    </ScrollView>