Android 每次打开键盘时,背景图像都会调整大小

Android 每次打开键盘时,背景图像都会调整大小,android,scroll,background-image,Android,Scroll,Background Image,我的应用程序中有一个“活动”和一个片段。该“活动”包含一个RelativeLayout,它是片段的持有者。我将一个可绘制图像设置为RelativeLayout的背景。单击编辑文本后,键盘打开,从而使RelativeLayout的背景图像调整大小 代码: AndroidManifest.xml: <activity android:name=".MainActivity" android:windowSoftInputMode="stateVisible|adjustResi

我的应用程序中有一个“活动”和一个片段。该“活动”包含一个RelativeLayout,它是片段的持有者。我将一个可绘制图像设置为RelativeLayout的背景。单击编辑文本后,键盘打开,从而使RelativeLayout的背景图像调整大小

代码: AndroidManifest.xml:

<activity
    android:name=".MainActivity"
    android:windowSoftInputMode="stateVisible|adjustResize">

如果删除“adjustResize”,则无法滚动视图

holder_layout.xml:

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

<ScrollView android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    >
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">
        <ImageView
        android:layout_width="@dimen/img_wdth"
        android:layout_height="@dimen/img_hght"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="15dp"
        android:id="@+id/usr_name_img"
        android:src="@drawable/user"
        android:tint="#ffffff"
        android:layout_marginTop="@dimen/img_mrgn_top"
        />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="@dimen/txt_height"
        android:id="@+id/name"
        android:textColor="#ffffff"
        android:layout_toRightOf="@+id/usr_name_img"
        android:background="@drawable/edittext_bg"
        android:layout_marginStart="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginEnd="20dp"
        android:layout_marginTop="@dimen/lyt_mrgn_top"
        android:hint="Your name"
        android:textColorHint="#ffffff"
        android:paddingLeft="30dp"
        android:inputType="textMultiLine" />
    <ImageView
        android:layout_width="@dimen/img_wdth"
        android:layout_height="@dimen/img_hght"
        android:layout_below="@+id/name"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="15dp"
        android:id="@+id/place_img"
        android:src="@drawable/location"
        android:tint="#ffffff"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="@dimen/txt_height"
        android:layout_below="@+id/name"
        android:id="@+id/locality"
        android:background="@drawable/edittext_bg"
        android:textColor="#ffffff"
        android:layout_marginTop="10dp"
        android:layout_marginRight="20dp"
        android:layout_marginEnd="20dp"
        android:hint="Locality"
        android:textColorHint="#ffffff"
        android:paddingLeft="30dp"
        android:layout_alignLeft="@+id/name"
        android:layout_alignStart="@+id/name"
        android:inputType="textMultiLine" />
    <ImageView
        android:layout_width="@dimen/img_wdth"
        android:layout_height="@dimen/img_hght"
        android:id="@+id/email_img"
        android:src="@drawable/email"
        android:tint="#ffffff"
        android:layout_below="@+id/locality"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="15dp"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="@dimen/txt_height"
        android:layout_below="@+id/locality"
        android:id="@+id/email"
        android:textColor="#ffffff"
        android:background="@drawable/edittext_bg"
        android:inputType="textEmailAddress"
        android:layout_marginTop="10dp"
        android:hint="E-Mail"
        android:textColorHint="#ffffff"
        android:paddingLeft="30dp"
        android:layout_alignLeft="@+id/locality"
        android:layout_alignStart="@+id/locality"
        android:layout_alignRight="@+id/locality"
        android:layout_alignEnd="@+id/locality" />
    <ImageView
        android:layout_width="@dimen/img_wdth"
        android:layout_height="@dimen/img_hght"
        android:layout_below="@+id/email"
        android:src="@drawable/phone"
        android:tint="#ffffff"
        android:id="@+id/mobile_img"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="20dp"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="@dimen/txt_height"
        android:layout_below="@+id/email"
        android:id="@+id/phone"
        android:background="@drawable/edittext_bg"
        android:layout_marginTop="10dp"
        android:hint="Mobile No."
        android:textColor="#ffffff"
        android:inputType="phone"
        android:textColorHint="#ffffff"
        android:paddingLeft="30dp"
        android:layout_alignLeft="@+id/email"
        android:layout_alignStart="@+id/email"
        android:layout_alignRight="@+id/email"
        android:layout_alignEnd="@+id/email" />
    <ImageView
        android:layout_width="@dimen/img_wdth"
        android:layout_height="@dimen/img_hght"
        android:layout_below="@+id/phone"
        android:src="@drawable/event"
        android:tint="#ffffff"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="20dp"
        android:id="@+id/imageView2" />
    <Spinner
        android:layout_width="match_parent"
        android:layout_height="@dimen/txt_height"
        android:gravity="center"
        android:overlapAnchor="false"
        android:background="@drawable/edittext_bg"
        android:popupBackground="#BB000000"
        android:layout_below="@+id/phone"
        android:spinnerMode="dropdown"
        android:id="@+id/eventspinner"
        android:layout_marginTop="10dp"
        android:layout_alignLeft="@+id/phone"
        android:layout_alignStart="@+id/phone"
        android:layout_alignRight="@+id/phone"
        android:layout_alignEnd="@+id/phone"></Spinner>
<EditText
        android:layout_width="match_parent"
        android:layout_height="@dimen/txt_height"
        android:id="@+id/bdgt"
        android:inputType="numberDecimal"
        android:hint="Your Budget"
        android:textColorHint="#ffffff"
        android:paddingLeft="25dp"
        android:textColor="#ffffff"
        android:layout_below="@+id/eventspinner"
        android:background="@drawable/edittext_bg"
        android:layout_marginTop="10dp"
        android:layout_alignLeft="@+id/eventspinner"
        android:layout_alignStart="@+id/eventspinner"
        android:layout_alignRight="@+id/eventspinner"
        android:layout_alignEnd="@+id/eventspinner" />
    <Button
        android:layout_width="match_parent"
        android:layout_height="@dimen/btn_height"
        android:id="@+id/book"
        android:background="@drawable/btn_bg"
        android:layout_centerHorizontal="true"
        android:text="Book Now"
        android:textColor="#ffffff"
        android:layout_marginLeft="90dp"
        android:layout_below="@+id/noOfPeople"
        android:layout_marginTop="40dp"/>
<ImageView
        android:layout_width="10dp"
        android:layout_height="@dimen/drp_dwn_arrw_hght"
        android:layout_below="@+id/phone"
        android:layout_alignParentRight="true"
        android:layout_marginRight="25dp"
        android:layout_marginTop="25dp"
        android:src="@drawable/downarrow"
        android:tint="#ffffff"
        android:id="@+id/imageView" />
<ImageView
        android:layout_width="@dimen/img_wdth"
        android:layout_height="@dimen/img_hght"
        android:id="@+id/bdgt_img"
        android:src="@drawable/budget"
        android:tint="#ffffff"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="20dp"
        android:layout_below="@+id/eventspinner"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="@dimen/txt_height"
        android:id="@+id/noOfPeople"
        android:layout_marginTop="10dp"
        android:background="@drawable/edittext_bg"
        android:hint=" Number ofpeople"
        android:paddingLeft="25dp"
        android:textColorHint="#ffffff"
        android:textColor="#ffffff"
        android:inputType="number"
        android:layout_toRightOf="@+id/group"
        android:layout_below="@+id/bdgt"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"/>

    <ImageView
        android:layout_width="@dimen/img_wdth"
        android:layout_height="@dimen/img_hght"
        android:id="@+id/group"
        android:tint="#ffffff"
        android:src="@drawable/group"
        android:layout_marginTop="20dp"
        android:layout_below="@+id/bdgt"
        android:layout_alignLeft="@+id/bdgt_img"
        android:layout_alignStart="@+id/bdgt_img" />
</RelativeLayout>
</ScrollView>

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
>
    <android.support.v7.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:id="@+id/cstm_toolbar"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:background="#AB47BC">
    </android.support.v7.widget.Toolbar>


    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
             android:orientation="vertical"
            android:background="@drawable/bg"
            android:layout_centerHorizontal="true"
            android:id="@+id/frag_holder"
            android:layout_below="@+id/cstm_toolbar"
        ></LinearLayout>
</RelativeLayout>

试试看,希望这能对你有所帮助

<RelativeLayout
   android:layout_width="match_parent"  -->@dimen/img_wdth
   android:layout_height="match_parent"  -->@dimen/img_hght
    xmlns:android="http://schemas.android.com/apk/res/android">

<ImageView
    android:layout_width="@dimen/img_wdth"
    android:layout_height="@dimen/img_hght"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="15dp"
    android:id="@+id/usr_name_img"
    android:src="@drawable/user"
    android:tint="#ffffff"
    android:layout_marginTop="@dimen/img_mrgn_top"
    />    
@dimen/img\u wdth
android:layout\u height=“match\u parent”-->@dimen/img\u hght
xmlns:android=”http://schemas.android.com/apk/res/android">

并使用“adjustPan”在清单中进行尝试。

在清单文件中使用此选项

android:windowSoftInputMode="adjustResize|stateAlwaysHidden"
然后从Xml文件中删除背景图像,并使用下面的方法通过Java代码进行设置

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

请尝试使用android:configChanges=“orientation | screenSize”这与方向无关。使用此
android:WindowsofInputMode=“stateVisible | adjustPan”
。键盘打开时背景图像会调整大小opened@Ironman我试过了,但是无法滚动视图我想知道为什么在使用键盘之前图像会调整大小opened@jobin无论何时使用
adjustResize
您的图像都可以放在可用空间内。当您使用
adjustPan
时,它会将整个布局移到顶部,这会给用户带来不好的体验,因此您必须通过编程方式使用
setBackgroundDrawableResource