软件键盘在Android上调整背景图像的大小

And*_*ong 127 android view android-linearlayout

每当出现软件键盘时,它都会调整背景图像的大小.请参阅下面的屏幕截图:

如你所见,背景有点挤压.任何人都可以了解为什么背景调整大小?

我的布局如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/page_bg"
    android:isScrollContainer="false"
>
    <LinearLayout android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
    >
        <EditText android:id="@+id/CatName"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:inputType="textCapSentences"
            android:lines="1"
        />
        <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/save"
            android:onClick="saveCat"
        />
    </LinearLayout>
    <ImageButton
        android:id="@+id/add_totalk"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:background="@null"
        android:src="@drawable/add_small"
        android:scaleType="center"
        android:onClick="createToTalk"
        android:layout_marginTop="5dp"
    />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

And*_*ong 187

好的,我通过使用修复它

android:windowSoftInputMode="stateVisible|adjustPan"
Run Code Online (Sandbox Code Playgroud)

里面的条目<Activity >标签的manifest文件.我认为这是由于在Activity中有ScrollView引起的.

  • 这还不够.至少,如果你不使用ScrollView.这里的解决方案要好得多http://stackoverflow.com/a/29750860/2914140或http://stackoverflow.com/a/18191266/2914140. (4认同)
  • 但是scrollview现在还没有工作.是以任何方式避免它 (3认同)
  • 问题是滚动时,使用adjustPan时,不工作...(如果你有一个ScrollView),这很烦人...... (2认同)

小智 104

我在开发聊天应用程序,聊天屏幕和背景图像时遇到了同样的问题.android:windowSoftInputMode="adjustResize"在显示软键盘后,挤压我的背景图像以适应可用空间,并且"adjustPan"将整个布局向上移动以调整软键盘.此问题的解决方案是在活动XML中设置窗口背景而不是布局背景.用于getWindow().setBackgroundDrawable()您的活动.

  • 很好!最佳答案!adjustPan导致附带效果,如:键盘覆盖组件但不显示滚动条. (3认同)
  • 只要背景图像不包含相关信息,@ parulb或其他任何恰好阅读此内容的人都会很好(并且谢谢你).我不时遇到的问题是我的背景将包含类似徽标的内容,并且将窗口背景设置为图像隐藏了ActionBar后面的徽标.对于某些屏幕,这不是问题,但有时我需要保留ActionBar(不隐藏).在设置窗口背景以考虑ActionBar时如何处理某种填充的任何想法? (3认同)

Ahs*_*rsi 33

这是避免此类问题的最佳解决方案.

第1步:创建样式

<style name="ChatBackground" parent="AppBaseTheme">
    <item name="android:windowBackground">@drawable/bg_chat</item>
</style>
Run Code Online (Sandbox Code Playgroud)

第2步:AndroidManifest文件中设置您的活动样式

<activity
    android:name=".Chat"
    android:screenOrientation="portrait"
    android:theme="@style/ChatBackground" >
Run Code Online (Sandbox Code Playgroud)


Jos*_*wan 27

通过android:windowSoftInputMode ="adjustPan"给出糟糕的用户体验,因为通过整个屏幕进入顶部(转移到顶部)所以,以下是最好的回答之一.

我有同样的问题,但在那之后,我从@Gem找到了很棒的回答

在清单中

android:windowSoftInputMode="adjustResize|stateAlwaysHidden"
Run Code Online (Sandbox Code Playgroud)

在xml中

不要在此处设置任何背景并在ScrollView下保持您的视图

在Java中

您需要将背景设置为窗口:

    getWindow().setBackgroundDrawableResource(R.drawable.bg_wood) ;
Run Code Online (Sandbox Code Playgroud)

感谢@Gem.


Bol*_*rme 14

只是为了补充......

如果你有关于你的活动的列表视图,你需要android:isScrollContainer="false"在listview属性中添加它...

并且不要忘记android:windowSoftInputMode="adjustPan"在您的活动中添加清单xml ...

如果你们android:windowSoftInputMode="adjustUnspecified"在你的布局上使用可滚动视图,那么你的背景仍将通过软键盘重新调整大小......

如果你使用"adjustPan"值来防止你的背景调整大小会更好...

  • android:windowSoftInputMode ="adjustPan"使整个视图随键盘移动.通常我们在屏幕顶部有一个标题.使用此标志它也会离开可见区域,这会带来糟糕的用户体验. (2认同)

kon*_*mik 7

如果有人需要adjustResize行为并且不希望他的ImageView大小调整,这是另一种解决方法.

只要把ImageView里面ScrollView=> RelativeLayoutScrollView.fillViewport = true.

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

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

        <FrameLayout
            android:layout_width="100dp"
            android:layout_height="100dp">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="fitXY"
                android:src="@drawable/gift_checked" />

        </FrameLayout>
    </RelativeLayout>
</ScrollView>
Run Code Online (Sandbox Code Playgroud)


小智 5

我在处理我的应用程序时遇到了主要问题。一开始,我使用@parulb 提供的方法来解决问题。非常感谢他。但是后来我注意到背景图像被操作栏部分隐藏了(我确定状态栏)。这个小问题已经由@zgc7009 提出,他在一年半前在@parulb 的回答下面发表了评论,但没有人回复。

折腾了一整天才找到办法,幸好现在手机上至少可以完美解决这个问题了。

首先,我们需要 drawable 文件夹中的 layer-list 资源来在顶部添加填充到背景图像:

<!-- my_background.xml -->
<?xml version="1.0" encoding="utf-8"?>

<layer-list
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:top="75dp">
        <bitmap android:src="@drawable/bg" />
    </item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)

其次,我们将此文件设置为背景资源,如上所述:

getWindow().setBackgroundDrawableResource(R.drawable.my_background);
Run Code Online (Sandbox Code Playgroud)

我正在使用 Nexus 5。我找到了一种方法来获取 xml 中操作栏的高度而不是状态栏,所以我必须使用固定高度 75dp 进行顶部填充。希望任何人都能找到这个拼图的最后一块。


Har*_*eet 5

在研究并实施了所有可用的答案之后,我在这里添加了一个解决方案。

这个答案是来自以下代码的组合:

/sf/answers/3193416201/

/sf/answers/1939154731/

这是自定义AppCompatImageView类,它显示没有拉伸或滚动 wrt 软键盘:-

public class TopCropImageView extends AppCompatImageView {

    public TopCropImageView(Context context) {
        super(context);
        setScaleType(ImageView.ScaleType.MATRIX);
    }

    public TopCropImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setScaleType(ImageView.ScaleType.MATRIX);
    }

    public TopCropImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setScaleType(ImageView.ScaleType.MATRIX);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        computeMatrix();
    }

    @Override
    protected boolean setFrame(int l, int t, int r, int b) {
        computeMatrix();
        return super.setFrame(l, t, r, b);
    }


    private void computeMatrix() {
        if (getDrawable() == null) return;
        Matrix matrix = getImageMatrix();
        float scaleFactor = getWidth() / (float) getDrawable().getIntrinsicWidth();
        matrix.setScale(scaleFactor, scaleFactor, 0, 0);
        setImageMatrix(matrix);
    }
}
Run Code Online (Sandbox Code Playgroud)

要将其用作我Fragment班级的背景,我将其设置为FrameLayout.

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

    <complete.package.TopCropImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@mipmap/my_app_background" />

    <!-- Enter other UI elements here to overlay them on the background image -->

<FrameLayout>
Run Code Online (Sandbox Code Playgroud)