【问题标题】:Fix a layout above soft keyboard修复软键盘上方的布局
【发布时间】:2018-12-01 21:34:11
【问题描述】:

我知道这个问题已经在互联网上流传很久了,但对我来说似乎没有任何效果。

怎么了

如屏幕截图所示,带有微调器的布局没有被向上推,而是被软键盘重叠。我希望这个布局在弹出时放在键盘上方。

我的布局文件

<?xml version="1.0" encoding="utf-8"?>
<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="@android:color/white"
    android:orientation="vertical"
    tools:context="believe.cht.speak.MainActivity">

    <ImageView
        android:id="@+id/logo"
        android:layout_width="64dp"
        android:layout_height="64dp"
        android:layout_gravity="center_horizontal"
        android:layout_margin="16dp"
        android:src="@drawable/ic_bubble"
        android:tint="@color/blue" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@android:color/transparent"
        android:gravity="top|start"
        android:hint="Start typing..."
        android:padding="8dp" />

    <ImageView
        android:id="@+id/shadow"
        android:layout_width="match_parent"
        android:layout_height="6dp"
        android:src="@drawable/shadow" />

    <RelativeLayout
        android:id="@+id/spinnerLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="@color/blue">

        <Spinner

            android:id="@+id/spinner"
            android:layout_width="200dp"
            android:layout_height="48dp"
            android:layout_alignParentEnd="true" />

    </RelativeLayout>

</LinearLayout>

我的尝试

android:windowSoftInputMode="adjustResize"

android:windowSoftInputMode="adjustPan" //in the manifest, also tried combining the two

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN); //in the activity

<item name="android:windowActionBarOverlay">true</item> //in styles.xml

【问题讨论】:

    标签: java android xml android-layout


    【解决方案1】:

    添加到上面 Nilesh 的答案中,即使代码有效,它也不适用于我的 Java 类。原因是我用

    隐藏了状态栏
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
    

    使用提供的代码并保持状态栏不变就可以完成这项工作。不知道为什么。如果有人能解释一下,我会很有帮助。

    【讨论】:

      【解决方案2】:

      尝试使用 RelativeLayout 而不是 LinearLayout 并将 android:alignParentBottom="true" 添加到您的 spinnerLayout

      【讨论】:

      • 我最初只使用RelativeLayout制作了这个布局,也试过了,没有运气。上面的答案完成了工作。
      【解决方案3】:

      试试这个

      <?xml version="1.0" encoding="utf-8"?>
      <ScrollView 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:fillViewport="true">
      
          <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:background="@android:color/white"
              android:orientation="vertical">
      
              <ImageView
                  android:id="@+id/logo"
                  android:layout_width="64dp"
                  android:layout_height="64dp"
                  android:layout_gravity="center_horizontal"
                  android:layout_margin="16dp"
                  android:src="@drawable/ic_message" />
      
              <EditText
                  android:layout_width="match_parent"
                  android:layout_height="0dp"
                  android:layout_weight="1"
                  android:background="@android:color/transparent"
                  android:gravity="top|start"
                  android:hint="Start typing..."
                  android:padding="8dp" />
      
              <ImageView
                  android:id="@+id/shadow"
                  android:layout_width="match_parent"
                  android:layout_height="6dp"
                  android:src="@drawable/ic_camera" />
      
              <RelativeLayout
                  android:id="@+id/spinnerLayout"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_gravity="bottom"
                  android:background="@color/colorPrimary">
      
                  <Spinner
      
                      android:id="@+id/spinner"
                      android:layout_width="200dp"
                      android:layout_height="48dp"
                      android:layout_alignParentEnd="true" />
      
              </RelativeLayout>
      
          </LinearLayout>
      </ScrollView>
      

      清单文件

          <activity android:name=".MyActivity"
              android:windowSoftInputMode="adjustResize">
              <intent-filter>
                  <action android:name="android.intent.action.MAIN" />
      
                  <category android:name="android.intent.category.LAUNCHER" />
              </intent-filter>
      
          </activity>
      

      输出

      【讨论】:

      • 你成就了我的一天!我也尝试将我的布局嵌套在 ScrollView 中,但没有使用 android:fillViewport 的东西,因为 android:layout_weight="1"
      猜你喜欢
      • 1970-01-01
      • 2016-04-20
      • 1970-01-01
      • 2018-08-28
      • 1970-01-01
      • 1970-01-01
      • 2015-11-01
      • 2016-10-14
      • 1970-01-01
      相关资源
      最近更新 更多