如果键盘可见,Android 布局背景会被压缩

标签 android android-layout background keyboard

我正在尝试编写代码来保持布局的大小,即使软键盘可见也是如此。但是只要键盘可见且布局看起来困惑,布局背景图像就会合并。但我在 whatsapp 中看到,即使键盘打开,背景也不会改变它的状态。所以,我不知道该怎么做。

我的代码:

package com.example.testlayout;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends ActionBarActivity {

String[] data = new String[50];

ListView list;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    getWindow().setBackgroundDrawableResource(R.drawable.image_bgswitch);
    list = (ListView) findViewById(R.id.listView1);


    for(int i=0;i<50;i++){
        data[i] = "";
    }

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
              android.R.layout.simple_list_item_1, android.R.id.text1, data);
    list.setAdapter(adapter);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
} }

XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.testlayout.MainActivity" >

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:layout_alignParentTop="true"
    android:layout_marginTop="0dp" >

</ListView>

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:ems="10" />

list :

<?xml version="1.0" encoding="utf-8"?>
   <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testlayout"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="21" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:windowSoftInputMode="adjustResize">

          <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

截图: 键盘不可见 enter image description here

enter image description here

键盘可见

这些是我的问题的上述代码和屏幕截图,请建议我即使键盘可见也能保持背景不变的技术。

最佳答案

在 list 中将 android:windowSoftInputModeadjustResize 更改为 adjustPan

<?xml version="1.0" encoding="utf-8"?>
   <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testlayout"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="21" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:windowSoftInputMode="adjustPan">

          <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

关于如果键盘可见,Android 布局背景会被压缩,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29676125/

相关文章:

java - 使用 setLayoutParams 循环 - ClassCastException

css 背景=包含 URL 更改无法跨浏览器工作

html - 图解说明 : only move the text and not the background+text

Android在状态栏后面绘制但不在导航栏后面

java - 在 Android ListView 中显示空行

android - 如何以编程方式查找 Android 设备的 MAC 地址

android - 像 Google Play 商店一样折叠工具栏

android - 如果我使用新的 "V7 Appcompat library ",我是否仍然需要 "V4 Support Library "才能获得最低 SDK = 7?

android - EditText setText() 在多个 EditText View 上设置文本

bash - "background(&) and wait"和在 bash 中执行普通命令有区别吗?