当滚动型内的布局比滚动型较大layout_weight不起作用 [英] layout_weight does not work when the layout inside a ScrollView is larger than the ScrollView

查看:303
本文介绍了当滚动型内的布局比滚动型较大layout_weight不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用线性布局的layout_weight的高度滚动视图内。然而,我发现,layout_weight只会与fillViewPort =真工作,如果没有为linear_weight展开足够的剩余空间。

我在下面列出了三个例子,我想打的TextView在#2相同的尺寸和#3作为XML级#1。
正如你可以在链接看到,#2,#3,TextView的是自动变更。

下面是我的例子。该TextViews和按钮包围的的LinearLayout内再滚动型内。

1。 Layout_weight正常使用相对于父。 (工作)

2。 Layout_weight工作相对于剩余的空间,而不是父母。(不工作)

3 Layout_weight忽略(不工作)

下面是我的XML code为#3。 #2,#1基本相同code少用或不用的按钮。

 <?XML版本=1.0编码=UTF-8&GT?;
<滚动型的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:ID =@ + ID /滚轮
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:weighSum =1
    机器人:fillViewport =真正的>    <的LinearLayout
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:方向=垂直
        机器人:weightSum =10>        <按钮
            机器人:layout_width =30dip
            机器人:layout_height =30dip
            机器人:文字=接受/>        <按钮
            机器人:layout_width =30dip
            机器人:layout_height =30dip
            机器人:文字=接受/>        <按钮
            机器人:layout_width =30dip
            机器人:layout_height =30dip
            机器人:文字=接受/>        <按钮
            机器人:layout_width =30dip
            机器人:layout_height =30dip
            机器人:文字=接受/>        <按钮
            机器人:layout_width =30dip
            机器人:layout_height =30dip
            机器人:文字=接受/>        <按钮
            机器人:layout_width =30dip
            机器人:layout_height =30dip
            机器人:文字=接受/>        <按钮
            机器人:layout_width =30dip
            机器人:layout_height =30dip
            机器人:文字=接受/>        <按钮
            机器人:layout_width =30dip
            机器人:layout_height =30dip
            机器人:文字=接受/>        <按钮
            机器人:layout_width =30dip
            机器人:layout_height =30dip
            机器人:文字=接受/>        <按钮
            机器人:layout_width =30dip
            机器人:layout_height =30dip
            机器人:文字=接受/>        <按钮
            机器人:layout_width =30dip
            机器人:layout_height =30dip
            机器人:文字=接受/>        <按钮
            机器人:layout_width =30dip
            机器人:layout_height =30dip
            机器人:文字=接受/>        <按钮
            机器人:layout_width =30dip
            机器人:layout_height =30dip
            机器人:文字=接受/>        <按钮
            机器人:layout_width =30dip
            机器人:layout_height =30dip
            机器人:文字=接受/>        <按钮
            机器人:layout_width =30dip
            机器人:layout_height =30dip
            机器人:文字=接受/>        <按钮
            机器人:layout_width =30dip
            机器人:layout_height =30dip
            机器人:文字=接受/>        <按钮
            机器人:layout_width =30dip
            机器人:layout_height =30dip
            机器人:文字=接受/>        <按钮
            机器人:layout_width =30dip
            机器人:layout_height =30dip
            机器人:文字=接受/>        <按钮
            机器人:layout_width =30dip
            机器人:layout_height =30dip
            机器人:文字=接受/>        <按钮
            机器人:layout_width =30dip
            机器人:layout_height =30dip
            机器人:文字=接受/>        <按钮
            机器人:layout_width =30dip
            机器人:layout_height =30dip
            机器人:文字=接受/>
        <的TextView
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =0dp
            机器人:layout_weight =6
            机器人:背景=#AAABBB
            机器人:文字=你好/>        <的TextView
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =0dp
            机器人:layout_weight =3
            机器人:背景=#BBBAAA            机器人:文字=你好/>        <的TextView
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =0dp
            机器人:layout_weight =5
            机器人:背景=#AAABBB            机器人:文字=你好/>
    < / LinearLayout中>< /滚动型>


解决方案

的权重正在按他们设计。也许你不希望他们的工作,而是说他们是不工作的方法是不准确的。想想你一秒钟,你会期望TextViews是在像素尺寸什么的情况下3次?权重是可用空间(父的不总大小)的百分比。滚动视图和的LinearLayout是,至少,你的屏幕大小,所有的孩子,否则意见的总和。所以,如果你的孩子的看法超出屏幕的大小(那些没有重量),多少空间你希望将要分配给剩余的意见(与0dp和权重的那些)?问题的关键是空间量是任意的,Android将不会尝试假设您想要多大的空间,因为它知道没有可能的方式。

TL; DR - 设置了minHeight您textviews,你有太多的按钮来处理的情况下(DP)

I'm trying to use a linear layout's layout_weight for the height inside of a scrollview. However I find that layout_weight would only work with fillViewPort=true and if there is enough remaining space for the linear_weight to expand.

I have listed three examples below and I want to make textView the same size in #2 and #3 as #1 in xml level. As you can see in the link, in #2, #3, textView was automatically changed.

Here are my examples. The TextViews and Buttons are enclosed inside of a LinearLayout and then inside of a scrollView.

1. Layout_weight working perfectly relative to the parent. (Working)

2. Layout_weight working relative to the remaining space and not the parent.(Not Working)

3 Layout_weight ignored (Not Working)

Here's my xml code for #3. #2 and #1 is basically the same code with less or no buttons.

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/scroller"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:weighSum="1"
    android:fillViewport="true" >

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

        <Button
            android:layout_width="30dip"
            android:layout_height="30dip"
            android:text="Accept" />

        <Button
            android:layout_width="30dip"
            android:layout_height="30dip"
            android:text="Accept" />

        <Button
            android:layout_width="30dip"
            android:layout_height="30dip"
            android:text="Accept" />

        <Button
            android:layout_width="30dip"
            android:layout_height="30dip"
            android:text="Accept" />

        <Button
            android:layout_width="30dip"
            android:layout_height="30dip"
            android:text="Accept" />

        <Button
            android:layout_width="30dip"
            android:layout_height="30dip"
            android:text="Accept" />

        <Button
            android:layout_width="30dip"
            android:layout_height="30dip"
            android:text="Accept" />

        <Button
            android:layout_width="30dip"
            android:layout_height="30dip"
            android:text="Accept" />

        <Button
            android:layout_width="30dip"
            android:layout_height="30dip"
            android:text="Accept" />

        <Button
            android:layout_width="30dip"
            android:layout_height="30dip"
            android:text="Accept" />

        <Button
            android:layout_width="30dip"
            android:layout_height="30dip"
            android:text="Accept" />

        <Button
            android:layout_width="30dip"
            android:layout_height="30dip"
            android:text="Accept" />

        <Button
            android:layout_width="30dip"
            android:layout_height="30dip"
            android:text="Accept" />

        <Button
            android:layout_width="30dip"
            android:layout_height="30dip"
            android:text="Accept" />

        <Button
            android:layout_width="30dip"
            android:layout_height="30dip"
            android:text="Accept" />

        <Button
            android:layout_width="30dip"
            android:layout_height="30dip"
            android:text="Accept" />

        <Button
            android:layout_width="30dip"
            android:layout_height="30dip"
            android:text="Accept" />

        <Button
            android:layout_width="30dip"
            android:layout_height="30dip"
            android:text="Accept" />

        <Button
            android:layout_width="30dip"
            android:layout_height="30dip"
            android:text="Accept" />

        <Button
            android:layout_width="30dip"
            android:layout_height="30dip"
            android:text="Accept" />

        <Button
            android:layout_width="30dip"
            android:layout_height="30dip"
            android:text="Accept" />


        <TextView
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="6"
            android:background="#AAABBB"
            android:text="hello" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="3"
            android:background="#BBBAAA"

            android:text="hello" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="5"
            android:background="#AAABBB"

            android:text="hello" />
    </LinearLayout>

</ScrollView>

解决方案

The weights are working as they were designed. Maybe not the way you want them to "work", but saying they are "not working" is inaccurate. Think about your 3rd case for a second, what size in pixels would you expect the TextViews to be? Weights are a percentage of the available space (not total size of the parent). The scrollview and linearlayout are, at a minimum, the size of your screen, otherwise the sum of all the children views. So if your children views exceed the size of the screen (the ones without weights), how much space do you expect to be allocated for the remaining views (the ones with 0dp and weights)? The point is that amount of space is arbitrary, android will not try to assume how much space you want, because it has no possible way of knowing.

TL;DR - set a minHeight (in dp) on your textviews to handle the case where you have too many buttons.

这篇关于当滚动型内的布局比滚动型较大layout_weight不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆