如何设置在不同设备上由于背景图像而扩展的滚动视图?

时间:2019-04-29 06:47:35

标签: android android-layout background-image android-scrollview

在我的应用中,我具有主布局,将线性布局作为父级和子级是滚动视图,而在此滚动视图中,我具有另一种线性布局。

例如

LinearLayout->

ScrollView->

LinearLayout->(在此处设置背景图像)

因此,如果背景分辨率非常高,它将扩大很多,因此我调整了背景大小,以便可以在大多数设备上使用,因为减少了扩展,但是在屏幕尺寸不同的设备中,尺寸仍然有很多差异。 因此问题仍然存在,解决该问题的正确方法是什么?

分辨率为(729 * 1296)的背景图像的屏幕是什么样的 Background Image(729*1296)

调整分辨率(400 * 711)的背景图像的大小并在xxhdpi设备上运行后,它看起来像这样。 Background image(400*711)

所以现在我有一点滚动。这样做很好,但是在不同的设备上应用可能会很棘手,并且布局尺寸会有所不同。

我希望在所有设备上都看起来像这样。 Background Image(What i want)

在不调整背景图像大小的情况下,我希望我的布局在所有设备中都看起来像这样,只有3个内部布局(图片,音频,视频)应该可见,其余部分应该滚动。

这是布局XML文件。

 @Override
    protected void init(VaadinRequest vaadinRequest) {
        final VerticalLayout layout = new VerticalLayout();

        Label video = new Label();
        video.setValue("<iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/dQw4w9WgXcQ\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen></iframe>"); // Replace this with your actual html
        video.setContentMode(ContentMode.HTML);

        layout.addComponents(video);

        setContent(layout);
    }

1 个答案:

答案 0 :(得分:0)

我刚刚看过布局代码。之所以显示这种行为,是因为您将背景分配给了高度为wrap_content的线性布局。因此,线性布局的大小会随所提供的背景而变化。您应该将背景放在图像视图中。

但是,我认为您应该更改布局的整体层次结构。应该是这样的:

RelativeLayout
    ImageView
    ScrollView
        LinearLayout-> this will contain your code, which you want to scroll

一个简单的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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="center"
        android:src="@drawable/background"/>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:fillViewport="true">

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

            <!-- Your Layout code goes here -->
        </LinearLayout>
    </ScrollView>
</RelativeLayout>