2012-03-14 175 views
1

我有一个布局,我希望组件填充屏幕的其余部分(使用权重)。 Listview完美工作。布局中嵌套的权重

在最后的2个按钮的工作方式,我打算他们也工作,但我得到一个警告说:“嵌套的权重是坏的表现”。

我认为这是一个问题,因为我已经使用了Listview的权重参数,因为当我删除listview警告消失了。

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="fill_parent" 
     android:orientation="vertical" > 

     <ListView 
      android:id="@+id/listMessages_messages" 
      android:layout_width="fill_parent" 
      android:layout_height="0dp" 
      android:layout_marginBottom="5dip" 
      android:layout_marginLeft="10dip" 
      android:layout_marginRight="10dip" 
      android:layout_marginTop="10dip" 
      android:layout_weight="1" 
      android:background="#000000" > 
     </ListView> 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" > 

      <Button 
       android:layout_width="fill_parent" 
       android:layout_height="0dp" 
       android:layout_weight="50" 
       android:visibility="invisible" /> 

      <Button 
       android:id="@+id/btnNewConversation_message" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginRight="5dip" 
       android:layout_weight="50" 
       android:onClick="newConversation" 
       android:text="@string/NewConversation" /> 
     </LinearLayout> 

    </LinearLayout> 
+0

所以你现在想要什么.. – 2012-03-14 10:44:58

+0

我想知道我做错了什么。又如,导致警告的是什么。因为我想在我的应用程序 – 2012-03-14 10:51:42

回答

1

检查这码。我不是在收到警告

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:weightSum="10"> 
    <ListView android:id="@+id/listMessages_messages" 
     android:layout_width="fill_parent" android:layout_height="0px" 
     android:layout_marginBottom="5dip" android:layout_marginLeft="10dip" 
     android:layout_marginRight="10dip" android:layout_marginTop="10dip" 
     android:layout_weight="8" android:background="#000000"> 
    </ListView> 
    <LinearLayout android:layout_width="fill_parent" 
     android:layout_height="0px" android:orientation="horizontal" 
     android:weightSum="10" android:layout_weight="2"> 
     <Button android:layout_width="0px" 
      android:layout_height="wrap_content" android:layout_weight="5" 
      /> 
     <Button android:id="@+id/btnNewConversation_message" 
      android:layout_width="0px" android:layout_height="wrap_content" 
      android:layout_marginRight="5dip" android:layout_weight="5" 
      android:onClick="newConversation" /> 
    </LinearLayout> 
</LinearLayout> 

当你把两种观点 1.List查看 2.Linear布局(两个按钮) 你需要给重属性正确并为按钮水平divison调整它们也给一些权重总和属性到第二个线性布局使用权重均分。注意第二个线性布局内我用宽度为按钮0px

+0

感谢您的回答,我无法弄清楚您在之前的回复中对权重的含义。现在我可以开始处理我的应用程序了。 – 2012-03-14 11:46:43

+1

如果您没有收到有关该代码的警告,那么您的IDE配置错误。嵌套权重恰好是嵌套的,只需添加weightSum来解决它们并不会改变这一事实。此外,我不明白为什么你应该使用fill_parent时使用所有的这些0px宽度/高度值,那有什么推理? – 2012-03-31 17:24:24

+0

@Justin Buser - 你能提供一个嵌套的权重的简单工作示例,它们不适合我,而且你听起来像你把它们弄糟了吗?谢谢。 – samosaris 2013-05-23 16:41:19

0

如果我理解你的问题正确,以下是溶胶: (比如一些10) 与顶面布置给予一定的权重的ListView像5或6 确保布局height属性设置为0px 创建子线性布局就给剩余重量(即列表视图的10-重量) 作为layout_weight儿童的布局儿童布局layout_height是0像素 在此布局地方两个按钮。希望这是你所需要的

+0

的多个位置使用这个方法,这与解决方案非常接近,但并不完全。在我看来,列表视图和按钮之间不应该有重量关系。列表视图的重量是让它垂直填充屏幕的其余部分(高度),并且按钮都必须水平填充屏幕的一半(宽度)。 – 2012-03-14 11:05:37

+0

换句话说,Listview的权重只适用于外部线性布局,而按钮的权重适用于它们所在的线性布局,并且不需要与列表视图进行任何交互。 – 2012-03-14 11:07:01

+4

你真的明白他想说什么吗?我已经阅读了五次,无法做出正面或反面的评论。 – 2012-03-31 17:14:32