修复 Scrollview Android 上的背景图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22460130/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:59:46  来源:igfitidea点击:

Fix background image on Scrollview Android

androidlayoutscrollview

提问by Mohammad Farahi

When I use Scrollview in my codes my background image stretches and scrolls with buttons. I want just buttons scroll, not the background. I have attached a related image that you can figure this out. Here is the XML file:

当我在我的代码中使用 Scrollview 时,我的背景图像会随着按钮拉伸和滚动。我只想滚动按钮,而不是背景。我附上了一张相关的图片,你可以弄清楚这一点。这是 XML 文件:

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

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


        <ImageButton
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:id="@+id/app"
                android:layout_gravity="center"
                android:background="@drawable/bluebutton"
                android:layout_marginTop="80dp"
                android:layout_marginRight="80dp"
                android:layout_marginLeft="80dp"

                />

        <ImageButton
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:id="@+id/moshiri"
                android:background="@drawable/bluebutton"
                android:layout_marginTop="8dp"
                android:layout_marginRight="80dp"
                android:layout_marginLeft="80dp"
                />
        <ImageButton
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"

                android:id="@+id/about"
                android:background="@drawable/bluebutton"
                android:layout_marginTop="8dp"
                android:layout_marginRight="80dp"
                android:layout_marginLeft="80dp"
                />
        <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"

                android:id="@+id/exit"
                android:background="@drawable/bluebutton"
                android:layout_marginTop="8dp"
                android:layout_marginRight="80dp"
                android:layout_marginLeft="80dp"
                />

    </LinearLayout>
</ScrollView>

enter image description here

在此处输入图片说明

回答by sachingajraj

This thing worked fine for me.

这件事对我来说很好用。

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >
        <ImageView
            android:id="@+id/gearImage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:clickable="true"
            android:src="@drawable/imageName" />

</ScrollView>

回答by NullPointerException

make 1 Linearlayout at root instead of scrollview and use android:background="@drawable/back"to that layout as

在 root 处创建 1 Linearlayout 而不是 scrollview 并将use android:background="@drawable/back"该布局设为

<LinearLayout 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/back" >

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

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@drawable/back"
            android:orientation="vertical" >


            <ImageButton
                android:id="@+id/app"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginLeft="80dp"
                android:layout_marginRight="80dp"
                android:layout_marginTop="80dp"
                android:background="@drawable/bluebutton" />

            <ImageButton
                android:id="@+id/moshiri"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="80dp"
                android:layout_marginRight="80dp"
                android:layout_marginTop="8dp"
                android:background="@drawable/bluebutton" />

            <ImageButton
                android:id="@+id/about"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="80dp"
                android:layout_marginRight="80dp"
                android:layout_marginTop="8dp"
                android:background="@drawable/bluebutton" />

            <ImageButton
                android:id="@+id/exit"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="80dp"
                android:layout_marginRight="80dp"
                android:layout_marginTop="8dp"
                android:background="@drawable/bluebutton" />
        </LinearLayout>
    </ScrollView>


</LinearLayout>

Here I am assuming that your @drawable/backis your given image background

在这里,我假设您@drawable/back是给定的图像背景

回答by Raanan

Set the background image on the ScrollView and add Padding to the ScrollView or Margins to the Linearlayout.

在 ScrollView 上设置背景图像,并将 Padding 添加到 ScrollView 或将 Margins 添加到 Linearlayout。

回答by Pablo Borba

This worked for me. The image was resized automatically and filled the entire background (Adjusted on width and cropped on height.)

这对我有用。图像自动调整大小并填充整个背景(调整宽度并裁剪高度。)

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scaleType="centerCrop"
            android:src="@drawable/app_back_image" />
    </RelativeLayout>

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:isScrollContainer="true">
        ...
    </ScrollView>
</RelativeLayout>

回答by Karthik Kompelli

Do this,

做这个,

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

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


        <ImageButton
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:id="@+id/app"
                android:layout_gravity="center"
                android:background="@drawable/bluebutton"
                android:layout_marginTop="80dp"
                android:layout_marginRight="80dp"
                android:layout_marginLeft="80dp"

                />

        <ImageButton
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:id="@+id/moshiri"
                android:background="@drawable/bluebutton"
                android:layout_marginTop="8dp"
                android:layout_marginRight="80dp"
                android:layout_marginLeft="80dp"
                />
        <ImageButton
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"

                android:id="@+id/about"
                android:background="@drawable/bluebutton"
                android:layout_marginTop="8dp"
                android:layout_marginRight="80dp"
                android:layout_marginLeft="80dp"
                />
        <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"

                android:id="@+id/exit"
                android:background="@drawable/bluebutton"
                android:layout_marginTop="8dp"
                android:layout_marginRight="80dp"
                android:layout_marginLeft="80dp"
                />

    </LinearLayout>
</ScrollView>

回答by Akhil Tripathi

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context=".CadetEnrollmentFormActivity">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:src="@drawable/background" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
<!--put your view here -->


            </LinearLayout>
        </LinearLayout>

    </ScrollView>
</FrameLayout>

回答by Pdroid

As Raanan said, set the background image on the ScrollView, but to prevent backround from streching on different screen size, you can add gravity to it.

正如 Raanan 所说,在 ScrollView 上设置背景图像,但为了防止背景在不同的屏幕尺寸上拉伸,您可以为其添加重力。

<?xml version="1.0" encoding="utf-8"?>
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/back"
android:gravity="top" />