【问题标题】:Layout not adjust when keyboard opens键盘打开时布局不调整
【发布时间】:2017-09-09 04:35:07
【问题描述】:

嗨,我在activity.xml 文件中有以下布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_change_password"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="0dp"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
android:layout_marginTop="0dp"
tools:context="com.kdpl.motodriver.ChangePassword">
<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar1"
    android:layout_width="fill_parent"
    android:layout_height="58dp"
    android:background="?attr/colorPrimary"
    android:minHeight="?attr/actionBarSize"
    android:titleTextColor="#ffffff">
    <Button
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:background="@drawable/back_arrow" android:onClick="onBackPressed" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/change_password_header"
        android:layout_gravity="center"
        android:id="@+id/toolbar_title"
        style="@style/TitleStyle"/>
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="30dp"
    android:layout_below="@+id/toolbar1"
    android:id="@+id/txt1">
    <EditText
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:id="@+id/txtOldPIN"
        android:hint="@string/txt_old_pin"
        android:maxLength="4"
        android:paddingLeft="10dp"
        android:textColor="#ffffff"
        android:inputType="numberPassword"
        android:textColorHint="@color/lightGray"
        android:gravity="center_vertical"/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:layout_below="@+id/txt1"
    android:id="@+id/txt2">
    <EditText
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:paddingLeft="10dp"
        android:textColor="#ffffff"
        android:id="@+id/txtNewPin"
        android:inputType="numberPassword"
        android:maxLength="4"
        android:hint="@string/txt_new_pin"
        android:textColorHint="@color/lightGray"
        android:gravity="center_vertical"/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:layout_below="@+id/txt2"
    android:id="@+id/txt3">
    <EditText
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:paddingLeft="10dp"
        android:id="@+id/txtConfirmPin"
        android:inputType="numberPassword"
        android:maxLength="4"
        android:textColor="#ffffff"
        android:layout_below="@+id/txt2"
        android:hint="@string/txt_confirm_new_pin"
        android:textColorHint="@color/lightGray"
        android:gravity="center_vertical"/>
</android.support.design.widget.TextInputLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_alignParentBottom="true">
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btnChangePIN"
        android:layout_marginRight="16dp"
        android:layout_marginLeft="16dp"
        android:layout_centerHorizontal="true"
        android:background="@drawable/my_custom_btn_1"
        android:layout_below="@id/txt3"
        style="@style/buttonStyle"
        android:text="@string/txt_change_pin"
        android:enabled="false"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btnCancelPIN"
        android:layout_marginTop="20dp"
        android:layout_marginRight="16dp"
        android:layout_marginLeft="16dp"
        android:layout_centerHorizontal="true"
        android:background="@drawable/my_custom_btn_1"
        android:layout_below="@id/btnChangePIN"
        style="@style/buttonStyle"
        android:text="@string/btn_cancel_text" />
</LinearLayout>

我在Manifest.xml中添加了以下行

  <activity android:name=".ChangePassword"
        android:configChanges="orientation|keyboardHidden"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="adjustResize"
        />

Buttons align parent bottom 在键盘打开时隐藏。布局不会自行调整大小。

我错过了什么?

更新视图

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_change_password"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="0dp"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
android:layout_marginTop="0dp"
tools:context="com.kdpl.motodriver.ChangePassword">
<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar1"
    android:layout_width="fill_parent"
    android:layout_height="58dp"
    android:background="?attr/colorPrimary"
    android:minHeight="?attr/actionBarSize"
    android:titleTextColor="#ffffff">
    <Button
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:background="@drawable/back_arrow" android:onClick="onBackPressed" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/change_password_header"
        android:layout_gravity="center"
        android:id="@+id/toolbar_title"
        style="@style/TitleStyle"/>
</android.support.v7.widget.Toolbar>
<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/toolbar1">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_below="@+id/toolbar1"
            android:id="@+id/txt1">
            <EditText
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                android:id="@+id/txtOldPIN"
                android:hint="@string/txt_old_pin"
                android:maxLength="4"
                android:paddingLeft="10dp"
                android:textColor="#ffffff"
                android:inputType="numberPassword"
                android:textColorHint="@color/lightGray"
                android:gravity="center_vertical"/>
        </android.support.design.widget.TextInputLayout>
        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_below="@+id/txt1"
            android:id="@+id/txt2">
            <EditText
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                android:paddingLeft="10dp"
                android:textColor="#ffffff"
                android:id="@+id/txtNewPin"
                android:inputType="numberPassword"
                android:maxLength="4"
                android:hint="@string/txt_new_pin"
                android:textColorHint="@color/lightGray"
                android:gravity="center_vertical"/>
        </android.support.design.widget.TextInputLayout>
        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_below="@+id/txt2"
            android:id="@+id/txt3">
            <EditText
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                android:paddingLeft="10dp"
                android:id="@+id/txtConfirmPin"
                android:inputType="numberPassword"
                android:maxLength="4"
                android:textColor="#ffffff"
                android:layout_below="@+id/txt2"
                android:hint="@string/txt_confirm_new_pin"
                android:textColorHint="@color/lightGray"
                android:gravity="center_vertical"/>
        </android.support.design.widget.TextInputLayout>
    </LinearLayout>
</ScrollView>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingBottom="10dp"
    android:layout_alignParentBottom="true">
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btnChangePIN"
        android:layout_marginRight="16dp"
        android:layout_marginLeft="16dp"
        android:layout_centerHorizontal="true"
        android:background="@drawable/my_custom_btn_1"
        android:layout_below="@id/txt3"
        style="@style/buttonStyle"
        android:text="@string/txt_change_pin"
        android:enabled="false"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btnCancelPIN"
        android:layout_marginTop="20dp"
        android:layout_marginRight="16dp"
        android:layout_marginLeft="16dp"
        android:layout_centerHorizontal="true"
        android:background="@drawable/my_custom_btn_1"
        android:layout_below="@id/btnChangePIN"
        style="@style/buttonStyle"
        android:text="@string/btn_cancel_text" />
</LinearLayout>

我已更新我的视图以在 toolbarbuttons 之间添加 scrollview。我还按照建议在我的活动中添加了getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);。它有效但并不完美。现在Buttonsscroll view 中的视图重叠。附上图片

【问题讨论】:

  • 添加滚动会帮助你。
  • 除了下面的布局外,还要在标签上方添加布局,以便您的滚动视图始终保持在两个指定视图之间

标签: android android-layout window-soft-input-mode


【解决方案1】:

添加父布局为滚动视图

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

【讨论】:

  • 如果我将父布局设置为scroll view,它不会全屏显示
【解决方案2】:

另一个答案是添加

android:windowSoftInputMode="adjustResize"

在你的根布局中

【讨论】:

    【解决方案3】:

    为什么不使用CoordinateLayout 作为父布局。如果它适用于零食,它也必须适用于键盘

    【讨论】:

      【解决方案4】:

      在您的活动中添加此代码:

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

      【讨论】:

        【解决方案5】:

        请使用 adjustPan 代替 adjustResize 如下:

        <activity android:name=".ChangePassword"
                android:configChanges="orientation|keyboardHidden"
                android:screenOrientation="portrait"
                android:windowSoftInputMode="adjustPan" />
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-03-05
          • 1970-01-01
          • 1970-01-01
          • 2018-06-12
          相关资源
          最近更新 更多