【问题标题】:Background image gets shrink on keyboard popup背景图像在键盘弹出窗口中缩小
【发布时间】:2018-05-14 01:05:02
【问题描述】:

我有一个带有背景图片的父布局,所以当键盘弹出时添加滚动视图后,背景图片会缩小。

如下图:

我检查了 SO 上的所有解决方案,将 windowSofInput 添加为 adjustResize 和 adjustPan 但 adjustResize 解决了问题,并且通过使用 adjustPan 无法向上滚动页面,滚动视图与调整 pan 一起使用。

我尝试添加一些滚动视图的属性,如 fillViewPort、adjustViewBounds 为 true。

我也试过 真的 使用此滚动视图不会缩小,但我不需要全屏,全屏时无法看到状态栏。

我尝试添加图像视图而不是将背景图像设置为父布局,并尝试设置它的 scaleType、matrix scale 类型减少但我的背景图像看起来不像原始图像。

看起来很像:

布局文件

<?xml version="1.0" encoding="utf-8"?>
<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"

    tools:context="com.example.admin.SmartCabFuelCard.RegisterDriver">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="matrix"
        android:src="@drawable/page"/>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/bg_color"
        android:alpha="0.5"/>

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:context="com.example.admin.SmartCabFuelCard.RegisterDriver"><!--

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


        <RelativeLayout
            android:id="@+id/rl"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerHorizontal="true"
            android:layout_marginEnd="@dimen/dimen_10dp"
            android:layout_marginStart="@dimen/dimen_10dp"
            android:layout_marginLeft="@dimen/dimen_10dp"
            android:layout_marginRight="@dimen/dimen_10dp">

            <ImageView
                android:id="@+id/logo"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="@dimen/dimen_20dp"
                android:src="@drawable/petrolpump" />

            <TextView
                android:id="@+id/register_driver_welcome"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:layout_below="@+id/logo"
                android:layout_marginTop="@dimen/dimen_15dp"
                android:padding="@dimen/dimen_10dp"
                android:text="@string/welcome_driver"
                android:textColor="@color/divider_color"
                android:textStyle="bold" />

            <EditText
                android:id="@+id/edt_mobile"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/register_driver_welcome"
                android:drawableLeft="@drawable/ic_local_phone"
                android:hint="Mobile No."
                android:inputType="number"
                android:maxLength="10"
                android:maxLines="1"
                android:padding="@dimen/dimen_15dp"
                android:textColor="@color/divider_color"
                android:textColorHint="@color/hint_color"
                android:textSize="@dimen/dimen_15dp" />

            <TextView
                android:id="@+id/register_driver_enter"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:layout_below="@+id/edt_mobile"
                android:padding="@dimen/dimen_10dp"
                android:text="@string/otp_send"
                android:textColor="@color/divider_color"
                android:textStyle="bold" />

            <EditText
                android:id="@+id/register_driver_otp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/register_driver_enter"
                android:layout_centerHorizontal="true"
                android:layout_gravity="center"
                android:layout_marginTop="@dimen/dimen_5dp"
                android:drawableLeft="@drawable/ic_lock_close"
                android:hint="OTP"
                android:inputType="number"
                android:maxLength="4"
                android:maxLines="1"
                android:padding="@dimen/dimen_15dp"
                android:textColor="@color/divider_color"
                android:textColorHint="@color/hint_color"
                android:textSize="@dimen/dimen_15dp" />


            <Button
                android:id="@+id/register_driver_register"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/register_driver_otp"
                android:layout_centerHorizontal="true"
                android:layout_marginBottom="@dimen/dimen_20dp"
                android:layout_marginTop="20dp"
                android:background="@color/colorPrimary"
                android:text="Register"
                android:textColor="@android:color/white" />

            <Button
                android:id="@+id/register_driver_sendotp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/edt_mobile"
                android:layout_centerHorizontal="true"
                android:layout_marginBottom="@dimen/dimen_40dp"
                android:layout_marginTop="20dp"
                android:background="@color/colorPrimary"
                android:text="Send OTP"
                android:textColor="@android:color/white" />

            <Button
                android:id="@+id/btn_resend"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignStart="@+id/register_driver_register"
                android:layout_below="@+id/register_driver_otp"
                android:layout_centerHorizontal="true"
                android:layout_marginBottom="@dimen/dimen_20dp"
                android:layout_marginTop="20dp"
                android:background="@color/colorPrimary"
                android:text="RESEND OTP"
                android:textColor="@android:color/white"
                android:visibility="gone" />
        </RelativeLayout>
    </RelativeLayout>
    </ScrollView>
</RelativeLayout>

谁能帮我解决这个问题?谢谢。

【问题讨论】:

  • 请发布您的布局 xml
  • 是添加请检查@Naz141
  • 也许这有帮助:stackoverflow.com/questions/16135984/…(使用框架布局并首先添加图像视图).. 还有一个原因为什么你将你的相对布局嵌套在另一个相对布局中(滚动视图-> 相对布局-> 相对布局)?
  • 是的,但这无助于它仍然会缩小。 @JasonSaruulo

标签: android image scrollview drawable window-soft-input-mode


【解决方案1】:

不要在 xml 中设置背景。在您的活动中将图像设置为窗口背景。

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

和清单条目

<activity
        android:name=".ActivityName"
        android:configChanges="screenSize|keyboard|orientation|screenSize"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme.NoActionBar"
        android:windowSoftInputMode="stateHidden" />

【讨论】:

  • 这是有效的.. 但一些图像隐藏在应用栏下方,因为它设置为窗口屏幕。 @ADM
  • 是的,因为它设置为 window 。如果您使用的是 AppBar,那么您可以在清单中使用 android:windowSoftInputMode="adjustNothing" 以 root 身份在 xml 中使用 Linearlayout。我发送此解决方案是因为您附加的图像未显示任何 AppBar 。
  • 我不能使用线性布局,我已经根据相对布局将视图设置为父级。我尝试将 adjustNothing 添加到 relative 但没有工作。
  • 你的布局看起来并不复杂它那么简单。如果放置线性布局解决问题,那么只需将其添加为根父布局。
  • 好的,我试过了,还是没解决,图片还在应用栏后面。
【解决方案2】:

如果你想设置背景图片,你应该在你的父 relativelayout 上设置它,而不是使用 imageview。

<?xml version="1.0" encoding="utf-8"?>
<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:background="@drawable/page"
tools:context="com.example.admin.SmartCabFuelCard.RegisterDriver">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/bg_color"
    android:alpha="0.5"/>

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

.....

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-18
    • 2017-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-26
    • 1970-01-01
    • 2011-07-15
    相关资源
    最近更新 更多