简体   繁体   English

软键盘显示时如何保持背景图像大小

[英]How to keep background image size when software keyboard show

When the software keyboard shows, it resizes my layout and thus squeezes the background image together. 当软件键盘显示时,它会调整我的布局大小,从而将背景图像压缩在一起。 My question is basically a duplicate of this question, which is explained in a very good way: 我的问题基本上是这个问题的重复,这是一个非常好的解释:

Software keyboard resizes background image on Android 软件键盘在Android上调整背景图像的大小

However, that question was closed when they found a hack to solve it. 但是,当他们找到解决问题的黑客时,这个问题就被关闭了。 I cannot use this hack. 我不能用这个黑客。 My entire layout is inside a ScrollView, and I need to be able to use this scrollview properly at all times. 我的整个布局都在ScrollView中,我需要能够始终正确使用此scrollview。 By using android:windowSoftInputMode="stateVisible|adjustPan" the user will not be able to scroll down and see the bottom of the screen while the keyboard is showing, since the layout will partly exist behind the keyboard. 通过使用android:windowSoftInputMode =“stateVisible | adjustPan”,用户将无法向下滚动并在键盘显示时看到屏幕底部,因为布局将部分存在于键盘后面。 Thus the solution is unacceptable to me. 因此,解决方案对我来说是不可接受的。 Are there any better solutions out there? 那里有更好的解决方案吗?

Cheers, 干杯,

I actually ran into a similar problem not too long ago. 我不久前遇到过类似的问题。 I stumbled upon the correct answer with a bit of work, though. 不过,我偶然发现了一些正确的答案。

In your android manifest for this project, attached to the specific activity that you are using, use the line android:windowSoftInputMode="adjustPan|stateVisible" in the activity tag. 在此项目的Android清单中,附加到您正在使用的特定活动,请在活动标记中使用android:windowSoftInputMode="adjustPan|stateVisible"

adjustPan basically means that your activity will not resize itself to fit the soft keyboard, and stateVisible means that the soft keyboard will show when requested (this can be stateAlwaysVisible , etc if necessary) adjustPan基本上意味着你的活动不会调整自身以适应软键盘,而stateVisible意味着软键盘会在请求时显示(如果需要,可以是stateAlwaysVisible等)

source : Android Dev for Activity tags 来源: Android Dev for Activity标签

After days of hardcore hacking I finally managed to construct a solution so advanced it might actually hurt to read it. 经过几天的硬核黑客攻击后,我终于设法构建了一个解决方案,所以它可能会让读者感到非常伤心。 I place an ImageView with the background behind the scrollview, and set scaleType="matrix" so it does not shrink when the keyboard is shown. 我将一个带有背景的ImageView放在scrollview后面,并设置scaleType =“matrix”,这样当键盘显示时它不会缩小。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/black"
    >
    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:src="@drawable/mah_kewl_background"
        android:scaleType="matrix"
        />
    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:fillViewport="true"
        >
        <RelativeLayout
            android:id="@+id/smsLayout"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            >
            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="PLUNX"
                />
        </RelativeLayout>
    </ScrollView>
</RelativeLayout>

You can use the windowBackground property, which makes the drawable fit the whole screen. 您可以使用windowBackground属性,这使得drawable适合整个屏幕。 To do that you need to: 要做到这一点,你需要:

1- Create a style: 1-创建样式:

<style name="Background" parent="@android:style/Theme.NoTitleBar">
    <item name="android:windowBackground">@drawable/you_bg_drawable</item>
</style>

2- Set your activity style in the AndroidManifest.xml 2-在AndroidManifest.xml中设置您的活动样式

<activity
    android:name=".ui.your_activity"
    android:theme="@style/Background"/>

I was googling for a solution to this exact problem when I came across this, and after doing some research I found a solution I think is slightly better. 当我遇到这个时,我正在谷歌上搜索这个确切问题的解决方案,经过一些研究后我发现了一个解决方案,我认为稍好一些。 Instead of hacking with a ImageView you can use this from your activity: 您可以在活动中使用此功能,而不是使用ImageView进行黑客攻击:

      getWindow().setBackgroundDrawableResource(R.drawable.bg_my_background);

Hope it helps anyone else searching for a solution for this. 希望它可以帮助其他人寻找解决方案。

Maybe there's another (really simple!) solution: In styles.xml: create a new style like this: 也许还有另一个(非常简单!)解决方案:在styles.xml中:创建一个这样的新样式:

<style name="Yourstyle" parent="AppBaseTheme">
    <item name="android:windowBackground">@drawable/mybackground</item>
</style>

AppBaseTheme is your normally used theme for your app. AppBaseTheme是您常用的应用主题。 Then you add in Manifest.xml: 然后添加Manifest.xml:

<activity 
    android:name="com.your.app.name.Activity"
    android:theme="@style/Yourstyle">
</activity>

So you have the same style but with a background. 所以你有相同的风格,但有背景。

Important: don't set any backgrounds in your normal .xml file! 重要提示:不要在普通的.xml文件中设置任何背景!

It's my first post and I hope it helps and sorry for my English. 这是我的第一篇文章,我希望它对我的英语有所帮助和抱歉。

I can't comment on your answer but I'd like to add something. 我不能评论你的答案,但我想补充一些东西。

I understand your dilemma and why the solution you linked to isn't a complete solution for your situation (since you can't scroll to the bottom of the view). 我理解您的困境以及为什么您链接的解决方案不是您的情况的完整解决方案(因为您无法滚动到视图的底部)。 I have a similar situation with a large scrollable EditText box. 我有一个类似的情况与一个大的可滚动EditText框。 When the soft keyboard pops up I don't like my background getting squished. 当弹出软键盘时,我不喜欢背景被压扁。

I have tried your solution and while at first glance it appears to work, there are some situations where it might not be ideal: 我已经尝试过你的解决方案,虽然乍一看似乎有效,但在某些情况下它可能并不理想:

  1. If you allow your app the have a landscape mode, the background will not resize/stretch 如果您允许应用程序采用横向模式,则背景将不会调整大小/拉伸
  2. If you run your app on a device with a larger screen or dpi, your background may not fill the screen 如果您在具有较大屏幕或dpi的设备上运行应用程序,则背景可能无法填满屏幕

Basically it seems that when you set the scaleType to matrix you're telling Android to draw the background 1:1 and not to scale. 基本上似乎当你将scaleType设置为matrix你会告诉Android以1:1绘制背景而不是缩放。 So what's happening when you open the soft keyboard using your solution is, your scrollView and imageView are all getting resized as usual, but the image src you set remains 1:1 thus continues showing the same area. 所以,发生了什么,当您使用您的解决方案,您打开软键盘scrollViewimageView都面临调整大小如常,但src时,您设定的图像保持1:1这样继续显示相同的区域。

So if for example your background image was set to a specific size (eg 480 x 800) and it perfectly fills your Nexus One, the moment you rotate to landscape you will have a black area along the right. 因此,例如,如果您的背景图像设置为特定尺寸(例如480 x 800)并且它完美地填充了您的Nexus One,那么当您旋转到横向时,您将在右侧有一个黑色区域。

This is moot, of course, if your background is simply a repeating pattern, in which case you can make it extremely large and hopefully compensate for various screens (maybe even tablets). 当然,如果你的背景只是一个重复的模式,这是没有实际意义的,在这种情况下,你可以使它非常大,并希望补偿各种屏幕(甚至平板电脑)。

Or you can also supply different backgrounds for different screen sizes/densities/orientations, but this still doesn't account for scaling. 或者您也可以为不同的屏幕尺寸/密度/方向提供不同的背景,但这仍然不考虑缩放。

In a vain effort to try and solve this, I stumbled upon a View attribute called android:isScrollContainer . 为了尝试解决这个问题徒劳无功,我偶然发现了一个名为android:isScrollContainerView属性。 This attribute tells certain views whether or not they are allowed to resize upon display of the soft keyboard. 此属性告诉某些视图是否允许它们在显示软键盘时调整大小。 This is also mentioned by someone in your linked solution. 您的链接解决方案中的某人也提到了这一点。 I have tried setting this (to false) on the ViewGroup containing my background, but any child element that scrolls seems override it causing the squishing again. 我已尝试在包含我的背景的ViewGroup上设置此(为假),但滚动的任何子元素都会覆盖它,导致再次压扁。

As of right now I don't think there is a complete solution for our situation. 截至目前,我不认为我们的情况有完整的解决方案。 Your's definitely works in some instances and I thank you for the effort. 你肯定在某些情况下工作,我感谢你的努力。 Hopefully Google will offer a solution in the future, but I don't think they would consider this a problem/priority. 希望Google将来会提供解决方案,但我认为他们不会认为这是一个问题/优先事项。

in Androidmanifest in activity tag use: 在Androidmanifest中的活动标签使用:

<activity ... android:windowSoftInputMode="adjustPan" ></activity>

check Android Developer reference 检查Android Developer参考

How about a solution where the 'background' is a Layout. 如何将“背景”作为布局的解决方案呢? Can we inflate a layout, make it the entire window size, then insert it as a window background? 我们可以膨胀布局,使其成为整个窗口大小,然后将其作为窗口背景插入吗?

After a lot of time searching... 经过大量的搜索...

Put android:windowSoftInputMode="adjustPan|stateVisible" in each Activitys 在每个Activity中放置android:windowSoftInputMode="adjustPan|stateVisible"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM