简体   繁体   中英

Android fixed background image in scrollview while scrolling

I have a situation where I need a scrollview to have a background image that shouldn't scroll along with it's parent when moving. Before any of you suggest me the links for setting background image and this that, I have already tried and it's not working.

The whole story goes like: I have an activity with fragments which have their own backgrounds with some input fields. When focusing over input fields, keyboard appears and background image squeezes. For that I put an image on background of scrollview that fixed my issue of squeezing background but raised another concern that background Image should stay static while scrolling the parent scrollview.

The second solution any of you may suggest is setting background of my activity rather playing with scrollview. That's right, but for that I had to make a style element with background of theme which appears odd while transitioning different fragments plus it adds overhead when I have a lot of code and fragments to move forward and back.

That's the point where I am stuck. I have gone through links below, if you just need to know that I tried it or not.

link1 link2 link3 ... and so on

below is the layout I am using for my fragments (it's all being done programmatically)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/top_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:scrollbars="none" >

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

        <ImageView
            android:id="@+id/backgroundView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:contentDescription="@string/app_name"
            android:scaleType="fitXY" />

        <LinearLayout
            android:id="@+id/parent_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
        </LinearLayout>
    </FrameLayout>
</ScrollView>
</LinearLayout>

So if you guys have any better solution keeping in mind the situation I have, will be warmly welcomed. Thank you

None of the above works for me with some reason but this things works.

getWindow().setBackgroundDrawableResource(R.mipmap.img_reg_bg);

put it in oncreate method hope it will help.

If I guess right you try to fix your hotfix to get it working propperly. If I'm right you sould go back to scratch and do it without your scrollview.

If you have a look at following Link: http://developer.android.com/guide/topics/manifest/activity-element.html#wsoft

you'll see softkeyboard mode can be adjusted. "adjustPan" should solve your problem.

"adjustPan"

The activity's main window is not resized to make room for the soft keyboard. Rather, the contents of the window are automatically panned so that the current focus is never obscured by the keyboard and users can always see what they are typing. This is generally less desirable than resizing, because the user may need to close the soft keyboard to get at and interact with obscured parts of the window.

Add someimgage to your drawable folderes, like drawable-mdpi , drawable-hdpi . In your LinearLayout "@+id/top_layout" add attribute:

android:background="@drawable/someimage"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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