Android:键盘弹出时背景图片的大小调整

34

我正在开发一个应用程序,当键盘弹出时,背景图片会收缩。我的.xml文件如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:facebook="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/background" >

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

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

            /**
              Other stuff 
            */

        </RelativeLayout>
    </ScrollView>
</RelativeLayout>

我在谷歌上搜索并发现,要添加

android:windowSoftInputMode="stateVisible|adjustPan"

在我的清单文件中写了,但没有用。

编辑:

键盘弹出之前,我的布局如下:

Image 1

键盘弹出后如下:

Image 2

请注意背景图像的差异。在第一张图片中,图像大小不变,在第二张图片中,背景图像缩小了。我需要在键盘弹出时将页脚和背景向上移动。

请告诉我我错过了什么或做错了什么。


adjustPan 应该正好满足您的需求(“活动的主窗口不会被调整大小以为软键盘腾出空间。相反,窗口的内容会自动平移”),请参阅文档。您遇到了什么问题? - matiash
为什么不将图像放在ScrollView的背景上?或者创建一个与之无关的ImageView,填充整个屏幕空间,并将其设置为背景,因为它是RelativeLayout。 - Elltz
请附上您的问题截图。 - Volodymyr Kulyk
11个回答

38

只需在您的 onCreate() 中使用此代码:

protected void onCreate(Bundle savedInstanceState) {
...   
 getWindow().setBackgroundDrawableResource(R.drawable.your_image_resource);
...
}

并删除您的xml中的此行:

android:background="@drawable/background"

详细信息请参阅:

http://developer.android.com/reference/android/view/Window.html


如果我想在这里使用比例类型,该怎么办?因为对于凹口屏幕,图像会被拉伸。 - Nil
1
@Nil 我不知道,过去三年我没有与Android一起工作。 - Adrian Cid Almaguer
但是您不能更改比例类型。 - famfamfam
本质上重复了在Android上调整软件键盘大小的背景图像,但加入了一些代码和建议以删除android:background - Martijn Pieters

10

我曾经遇到过类似的问题,我的问题是通过在清单文件中使用以下属性来解决的,请在声明活动的位置使用它。

 android:windowSoftInputMode="stateHidden|adjustResize|adjustPan"

顺便提一下,如果您不希望在活动启动时隐藏键盘,则可以跳过 stateHidden 部分。 - mushahid

9

嘿,你能试着添加这个吗?

android:isScrollContainer="false" 

在你的ScrollView中添加这段代码,它曾经解决了我的问题,也许会对你有帮助。

此外,在manifest.xml文件中的你的activity中添加以下内容:

android:windowSoftInputMode="adjustPan"

希望这有所帮助..!! :)

7

如果我理解您的问题正确,您想要一个带有背景和滚动视图的布局。当软键盘弹出时,您想要调整滚动视图的大小,但保持背景全尺寸,对吗?如果是这样,那么我可能已经找到了一个解决此问题的方法:

您可以创建2个活动。

活动1:

public class StartActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent StartApp = new Intent(this, DialogActivity.class);
    startActivity(StartApp);         // Launch your official (dialog) Activity

    setContentView(R.layout.start);  // your layout with only the background 
    }
}

Activity2:

public class DialogActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    this.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);  // get rid of dimming
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);  //get rid of title bar (if you want)

    setContentView(R.layout.dialog);  //Dialog Layout with scrollview and stuff

    Drawable d = new ColorDrawable(Color.BLACK);  //make dialog transparent
    d.setAlpha(0);
    getWindow().setBackgroundDrawable(d);
    }
}

开始布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/test">  //your background
</LinearLayout>

对话框布局:

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

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

            <LinearLayout
                android:orientation="vertical"     
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <!-- All your Scrollview Items -->

            </LinearLayout>
     </ScrollView>
</RelativeLayout>

AndroidManifest.xml

启动 Activity:

android:theme="@android:style/Theme.NoTitleBar.Fullscreen">

对话框活动

android:theme="@android:style/Theme.Dialog"
android:windowSoftInputMode="adjustResize"
android:excludeFromRecents="true"

编辑:要立即完成活动,请在DialogActivity中的某处使用以下代码(例如,重写返回按钮):

@Override
public void onBackPressed() {
    Intent intent = new Intent(getApplicationContext(), StartActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.putExtra("EXIT", true);
    startActivity(intent);
}

在StartActivity的onCreate()方法中:

if (getIntent().getBooleanExtra("EXIT", false)) {
    finish();
}
else {
    Intent StartApp = new Intent(this, TestActivity.class);
    startActivity(StartApp);
}

屏幕截图

正常情况下 Normal

点击EditText框 Clicked textbox

向下滚动后(注:我把文本框放在scrollview中,所以它消失了;) after scrolled down

希望这能帮到你 :)


6

我也遇到了同样的问题。

请按照以下的方式在onCreate方法中添加外部布局的背景:

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

并将您的外部布局添加为:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
>
<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#4d000000"
    ><other code></ScrollView></RelativeLayout>

android:layout_weight="1" 添加到外部布局中,并不要在xml文件中设置背景。我认为这对某些人有所帮助。

3
你应该将 android:background="@drawable/background" 放在 ScrollView 上面的 ImageView 中。当键盘弹出时,它会使屏幕变小,如果你设置了一个图片作为背景,你无法控制它如何被调整/裁剪。所以,假设你想让图片全宽但保持纵横比,请尝试以下方法:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:facebook="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:src="@drawable/background"
        android:scaleType="centerCrop" />

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >           
            /**
            Other stuff
            */
        </RelativeLayout>
    </ScrollView>
</RelativeLayout>

你可以尝试不同的android:scaleType值来实现你想要的效果。如果你希望图片从顶部裁剪而不是默认的中间裁剪,请看这里,了解如何实现这个效果,或者尝试使用这个库。

0

为什么不这样做呢?

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"

        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:fillViewport="true"
        android:focusable="true"
        android:background="@drawable/background"
        android:focusableInTouchMode="true">

        <RelativeLayout
            android:id="@+id/relative"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:focusable="true"
            android:focusableInTouchMode="true"
    /**
              Other stuff 
            */
    >
  </RelativeLayout>
  </ScrollView>

0

不要使用 android:windowSoftInputMode="stateVisible|adjustPan",而应该改为 android:windowSoftInputMode="stateVisible|adjustResize"。这对我有效。


我尝试过这个,但它不起作用,经过搜索我发现是由于我的布局中的滚动视图所致。 - Manoj Fegde
可能是因为你的AndroidManifest.xml文件,你能否复制你清单文件的代码并在这里粘贴? - Optimus NTL

0

使用ScrollView作为顶级父视图。 当您打开和关闭键盘时,它将自动上下滚动。


-1

在你的AndroidManifest.xml文件中,应该为ActivitywindowSoftInputMode设置使用adjustResize选项。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接