在scrollview中添加两个线性布局,布局可以收缩或压缩。在这里我在其中使用layout_weight,这就是发生问题的原因

问题描述 投票:0回答:2

当我在scrollview中使用单个线性布局时,输出和代码就是这样

Xml代码:

<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:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:weightSum="10">


            <ImageView
                android:id="@+id/disp_img"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="4"
                android:background="@color/grey"
                android:scaleType="fitXY" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_margin="10dp"
                android:layout_weight="5"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/disp_title"
                    style="@style/disTitleTextViewStyle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"></TextView>

                <TextView
                    android:id="@+id/disp_cont"
                    style="@style/disdiscTextViewStyle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp"></TextView>
            </LinearLayout>

            <LinearLayout

                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="@drawable/disptextdes"
                android:orientation="vertical"

                >

                <TextView
                    android:id="@+id/disp_readmore"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="30dp"
                    android:layout_marginTop="15dp"
                    android:text="To read more click on this"
                    android:textColor="@android:color/black"
                    android:textSize="15sp"></TextView>
            </LinearLayout>

            </LinearLayout>


    </ScrollView>
</RelativeLayout>

输出为:enter image description here

但是当我添加另一个布局时,代码和输出看起来像]

<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:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">
        //first linear layout
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:weightSum="10">


            <ImageView
                android:id="@+id/disp_img"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="4"
                android:background="@color/grey"
                android:scaleType="fitXY" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_margin="10dp"
                android:layout_weight="5"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/disp_title"
                    style="@style/disTitleTextViewStyle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"></TextView>

                <TextView
                    android:id="@+id/disp_cont"
                    style="@style/disdiscTextViewStyle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp"></TextView>
            </LinearLayout>

            <LinearLayout

                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="@drawable/disptextdes"
                android:orientation="vertical"

                >

                <TextView
                    android:id="@+id/disp_readmore"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="30dp"
                    android:layout_marginTop="15dp"
                    android:text="To read more click on this"
                    android:textColor="@android:color/black"
                    android:textSize="15sp"></TextView>
            </LinearLayout>
            // when i add second linear layout then the first linear layout is varied,I don't know why
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/citispotterlogo"></ImageView>
            </LinearLayout>
            </LinearLayout>


    </ScrollView>
</RelativeLayout>

输出为:enter image description here

当我以线性布局添加图像时,第一个线性布局缩小了,我不知道为什么?请帮助我解决这个问题,我尝试在Google Stackoverflow上的Google上进行搜索,但是我对此一无所获。任何人都可以解决这个问题,或者给我其他解决这个问题的方法。

所以我的最终输出看起来像这样我在滚动视图中添加了两个图像(https://i.stack.imgur.com/DpJ9C.png)(https://i.stack.imgur.com/QhQoL.png

android android-scrollview android-layout-weight
2个回答
0
投票
<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:weightSum="10">

此LinearLayout的高度为match_parent,您应该使用wrap_content或使用固定的高度,以便第二个布局必须可见


0
投票
当您添加最后一个LinearLayout时,

此布局在第一个线性布局中使用weightSum而不包含权重。这导致UI呈现不正确。

我已经提供了两个选项解决方案,你们俩都应该做。

选项1

将第一个LinearLayout中的加权和更改为11,然后将权重值1添加到最后一个LinearLayout。

选项2

向最后的LinearLayout添加一个加权值,但是您需要调整其余的加权值,以使其等于第一个LinearLayout(10)中的加权和。

版面重量查看

LinearLayout支持使用android:layout_weight属性为单个孩子分配权重。该属性根据视图应在屏幕上占据多少空间来为其分配“重要性”值。较大的权重值使其可以扩展以填充父视图中的所有剩余空间。子视图可以指定一个权重值,然后按其声明的权重比例将视图组中的所有剩余空间分配给子视图。默认权重为零。

© www.soinside.com 2019 - 2024. All rights reserved.