Android 布局权重在 ScrollView 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10312272/
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 03:14:46  来源:igfitidea点击:

Layout Weights do not work inside a ScrollView

androidandroid-layout

提问by MIP TECH

I want to assign layout weights to several items within a LinearLayoutinside of a ScrollView. However, the ScrollViewignores the LinearLayoutweightSum.

我想一个内指定布局权重的几个项目LinearLayout的内部ScrollView。但是,ScrollView忽略了LinearLayoutweightSum.

My goal is to divide the layout with weights of 2, 1, 1 (for a total sum of 4), but this does not work properly inside of a ScrollView.

我的目标是用 2、1、1 的权重划分布局(总和为 4),但这在ScrollView.

How can I solve this layout problem?

我该如何解决这个布局问题?

main.xml

主文件

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" 
        android:weightSum="4">

        <LinearLayout android:id="@+id/logo"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" 
            android:layout_weight="2"
            android:background="#FFFFFF" />

        <LinearLayout android:id="@+id/logo1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" 
            android:layout_weight="1"
            android:background="#000000" />


        <LinearLayout android:id="@+id/logobutton"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" 
            android:layout_weight="1" 
            android:background="#4B4B4B" />

    </LinearLayout>
</ScrollView>

回答by Akhil

I have faced this problem before. Just use android:fillViewport="true"in your ScrollView and it will fill up the screen.

我以前遇到过这个问题。只需android:fillViewport="true"在您的 ScrollView 中使用,它就会填满屏幕。

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true" >

回答by David Scott

This won't work as you have done it. The child view of a ScrollViewshould be set to wrap_content. If you set it to fill_parent, it will fill the area of the ScrollViewand never scroll, because it won't be larger than the ScrollView.

这不会像你所做的那样工作。ScrollView的子视图应设置为wrap_content. 如果将其设置为fill_parent,它将填充ScrollView的区域并且从不滚动,因为它不会大于ScrollView

The idea of layout_weightis to fill a specific area proportionately.

的想法layout_weight是按比例填充特定区域。

You should set all of the child LinearLayoutslayout_heightto either wrap_contentor a specific size (in dp) and the parent LinearLayoutlayout_heightto wrap_content

您应该所有子集LinearLayoutslayout_height要么wrap_content或特定尺寸(DP)和父的LinearLayoutlayout_heightwrap_content

As said you need to remove the additional

如上所述,您需要删除额外的

xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:android="http://schemas.android.com/apk/res/android"

if it's not the root (first) element of the layout.

如果它不是布局的根(第一个)元素。

回答by Andy Sin

In my experience, fillviewport=trueworks bit strange.

根据我的经验,fillviewport=true工作有点奇怪。

I solved this problem by wrapping LinearLayoutwith another Layout like FrameLayout.

我通过LinearLayout用另一个 Layout 之类的 Layout包装解决了这个问题FrameLayout

I hope this helps.

我希望这有帮助。

回答by Manju S B

Add below line in your ScrollView it will be work fine.

在您的 ScrollView 中添加以下行,它将正常工作。

Only single child must be there for ScrollView

ScrollView 必须只有一个孩子

android:fillViewport="true"

回答by Ali Keb

just put this in your scroll view:

把它放在你的滚动视图中:

android:fillViewport="true"

android:fillViewport="true"