理解androidxml布局的问题

理解androidxml布局的问题,android,xml,android-linearlayout,Android,Xml,Android Linearlayout,我真的试过了,但我不明白Android是如何解释layou-weight设置的 我想要达到的是 顶部有固定高度的收割台 底部包含编辑文本和按钮的输入区域 中间的一个内容部分,它拿走了所有剩下的空间 键入时,我希望将编辑文本增加到特定高度,并在输入的文本超过可用高度时开始滚动。为此,我需要周围的LinearLayout与编辑文本一起增长 如果我为内部LinearLayout定义一个特定高度,它将不会增长。如果我不这样做,内部布局将占用所有空间,而不是滚动视图,无论我尝试使用布局\u权重:( 我

我真的试过了,但我不明白Android是如何解释
layou-weight
设置的

我想要达到的是

  • 顶部有固定高度的收割台
  • 底部包含编辑文本和按钮的输入区域
  • 中间的一个内容部分,它拿走了所有剩下的空间
键入时,我希望将编辑文本增加到特定高度,并在输入的文本超过可用高度时开始滚动。为此,我需要周围的
LinearLayout
与编辑文本一起增长

如果我为内部
LinearLayout
定义一个特定高度,它将不会增长。如果我不这样做,内部布局将占用所有空间,而不是滚动视图,无论我尝试使用
布局\u权重
:(

我当前的XML如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="I'm a header" />
    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:gravity="bottom">
        <LinearLayout
            android:id="@+id/itemContainer"
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
    </ScrollView>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:padding="2dp"
        android:gravity="bottom"
        android:isScrollContainer="true"
        android:background="@drawable/steel" >
        <EditText
            android:id="@+id/edittext01"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.2"
            android:scrollbars="vertical"
            android:fadingEdge="vertical"
            android:fadingEdgeLength="60dp"
            android:maxHeight="40dp"
            android:textSize="15sp" />
        <Button
            android:id="@+id/button"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.8"
            android:layout_gravity="right"
            android:text="Send"
            android:textStyle="bold"
            android:textSize="15sp" />
    </LinearLayout>
</LinearLayout>


非常感谢您提供任何提示!

我相信您最好使用RelativeLayout。添加标题,并将其设置为
layout\u alignParentTop=“true”
。然后添加编辑文本和按钮,将该区域设置为
layout\u alignParentBottom=“true”
。然后,添加中心区域,将宽度和高度设置为
fill\u parent
,并在上面设置
layout=“{EditText/按钮布局的id}”
,在下面设置
layout=“{header布局的id}”


我没有Eclipse在工作,否则我会为您测试它,但这应该会起作用。

layout\u weight
用于确定在各种小部件获得所需的所有空间后,Android如何分配剩余空间

假设你有3个小部件排成一行,每个小部件需要10个像素,但是你有50个像素的空间。下面是一些布局权重和每个小部件需要的像素数的示例:

widget1: weight = 1, 30px
widget2: weight = 0, 10px
widget3: weight = 0, 10px

widget1: weight = 1, 20px
widget2: weight = 1, 20px
widget3: weight = 0, 10px

widget1: weight = 1, 16.5px
widget2: weight = 1, 16.5px
widget3: weight = 1, 16.5px
您可以将权重视为可用空间的百分比。它将所有值相加,然后根据需要分配部分。因此,如果有帮助,您可以使用加起来为100的权重来计算百分比:

widget1: weight = 0, 10px
widget2: weight = 25, 15px
widget3: weight = 75, 25px
如果我理解正确,您希望底部小部件以特定大小开始,然后在必要时扩展到某个最大大小,然后滚动

而且,您希望上面的小部件占据所有额外的空间

不幸的是,在Android中,不可能指定最大大小。我认为最好是给ScrollView一些固定的大小,布局权重=1,并告诉底部的LinearLayout包装内容。这(我认为)导致LinearLayout展开,直到无法再展开为止,因为ScrollView已经占用了空间


然而,challange为ScrollView指定了一个固定的大小,在所有不同的屏幕大小和分辨率下都是有意义的。你可能最终不得不为不同的分辨率创建不同的布局,这可能不值得。就个人而言,我只是给editText一个固定的大小…

我也遇到了类似的问题(一个页脚,刷新时间戳贴在屏幕底部,ListView/ScrollView占据了所有剩余空间),经过多次尝试后,我使用了以下布局:

<RelativeLayout 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:layout_above="@+id/refresh_date_text" 
              android:layout_alignParentTop="true"
              android:orientation="vertical" 
              >

    <ListView android:id="@android:id/list" android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:background="#FFFFFF"
        android:layout_weight="1" android:divider="@drawable/list_divider"
        android:dividerHeight="1dp" android:drawSelectorOnTop="false" />

    <TextView android:id="@android:id/empty"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="#ffffff"
              android:gravity="center_horizontal"
              android:paddingTop="10dp"
              android:text="The list is empty."/>

</LinearLayout>

<TextView android:id="@+id/refresh_date_text"
          android:layout_width="match_parent" 
          android:layout_height="wrap_content" 
          android:layout_alignParentBottom="true"
          android:gravity="center_horizontal" 
          android:background="#f0f0f0" 
          android:textStyle="italic" 
          android:textSize="12dp"
          />
</RelativeLayout>

使用滚动条时无需剪裁列表视图,在所有方向上完全对齐,正确显示“列表为空”文本。



这会很有帮助的

非常感谢,KCopock!我会在一两个小时内测试它,并让你知道它是否有效。嗯,它没有真正改变任何东西。只有标题消失了。;)如果我再次使用LinearLayout,标题会回来。我保留了“layout\u alignParentTop=“true”和“layout\u alignParentBottom=“true”'正如您所建议的。当我将内部'LinearLayout'设置为'layout_height=“wrap_content'”时,它会占据半个屏幕。此外,我不理解为什么EditText会占据LinearLayout内80%的宽度,因为它被定义为'layout_weight=“0.2”“。但这不是这里的问题……;)你能把你试过的东西贴到pastebin.com链接上吗?或者只是把它编辑到原来的帖子里?我可以看看有没有什么东西跳出来。哦,还有,布局权重应该是整数。如果我没记错的话,所有定义的布局权重加起来应该是10。所以2的重量是20%。不过我可能错了。试试那个。它对我的XML注释做了一些奇怪的事情,但基本上我添加了四行代码。要使Eclipse编译,我所需要编辑的就是将页脚放在ScrollView之前,这样就可以找到它。现在页眉已经存在了,但页脚仍然占据了屏幕的50%,将按钮和编辑文本与顶部对齐。我感觉整体结构是正确的,但内部线性布局不是。非常感谢您迄今为止的帮助!:)谢谢你,玛拉。这正是我想做的。也许科科波克还有其他想法。如果必须的话,我将为不同的解决方案创建布局:(将ScrollView设置为特定大小并添加layout_weight=1将导致ScrollView占用其所能获得的所有空间并“收缩”)一旦其他小部件占用更多空间,它就会被定义为大小,对吗?听起来不错。我相信EditText有一个setMultiLine属性。如果你设置了它,并设置了包装内容的高度,我很确定它会扩展到合适的位置,并相应地缩小ScrollView。嘿,Mayra!我尝试了各种版本的设置ScrollView,一个Linearly输出等到特定的大小和布局_-weight=1,但如果我继续输入,不断增长的编辑文本甚至会从屏幕上增长出来!你认为我完全走错了方向?!没想到会在这上面花费数小时…;)从屏幕上增长出来,你的意思是它不会滚动?就这样离开了屏幕?听起来很奇怪。。正如我所说的,我个人只需将编辑文本设置为特定的开始大小,然后滚动,
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1"
>
<TextView
 android:text=" TextView " 
 android:textColor="#ffffff"
 android:textSize="15sp"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:background="#ff0000"
 />
 <EditText
  android:text=" EditText " 
  android:textSize="15sp"
  android:layout_width="250dp"
  android:layout_weight="1"
  android:layout_height="0dp"
  />
 <Button
    android:text=" button " 
    android:textColor="#000000"
    android:textSize="15sp"
    android:gravity="center_vertical|center_horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />
  </LinearLayout>