2013-10-16 174 views
0

我试图设计出这样我的UI: enter image description here嵌套布局重

问题是,当我添加ListView和设置其宽度match_parent,它采用了根布局,并采取了整个屏幕的宽度,即使它被包裹在另一个布局中,只占1/4宽度。有人可以告诉我一个方法如何完成画面上的画面吗?我还读到嵌套的layout_weight对性能不利,应该使用完全不同的东西吗?

XML文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center_horizontal" 
    android:orientation="horizontal" > 

    <RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_weight="3"> 
    </RelativeLayout> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     android:layout_weight="1" > 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" 
      android:layout_weight="13"> 

      <ListView 
       android:id="@+id/listView1" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" > 
      </ListView> 

     </LinearLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1"> 

      <Button 
       android:id="@+id/button1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Button" /> 

     </LinearLayout> 

    </LinearLayout> 

</LinearLayout> 

回答

0

更改您的右侧视图是一个RelativeLayout,设置你的按钮,在你的右手视图的底部,然后设置你的ListView财产android:layout_above="@id/button1"将允许你让你的ListView填充你的按钮不占用的剩余空间。我也删除了大量不必要的嵌套布局。

不要忘记,在使用布局权重时,您需要根据LinearLayout的方向指定宽度/高度的0dip。这可能就是你的ListView占据整个屏幕的原因。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center_horizontal" 
    android:orientation="horizontal" > 

    <RelativeLayout 
     android:layout_width="0dip" 
     android:layout_height="match_parent" 
     android:layout_weight="3" /> 

    <RelativeLayout 
     android:layout_width="0dip" 
     android:layout_height="match_parent" 
     android:layout_weight="1" > 

     <Button 
      android:id="@+id/button1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:text="Button" /> 

     <ListView 
      android:id="@+id/listView1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_above="@id/button1" /> 
    </RelativeLayout> 

</LinearLayout> 
1

看起来你有太多的ViewGroup给你的有什么几View秒。你或许可以改变这部分

<LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:layout_weight="13"> 

     <ListView 
      android:id="@+id/listView1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" > 
     </ListView> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1"> 

     <Button 
      android:id="@+id/button1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Button" /> 

    </LinearLayout> 

喜欢的东西

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:layout_weight="1" > 

     <ListView 
      android:id="@+id/listView1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" > 
     </ListView> 

     <Button 
      android:id="@+id/button1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Button" /> 

    </LinearLayout> 

我不认为额外LinearLayout是必要的。如果您需要Button的某些属性,例如layout_alignParentBottomlayout_above

,您也可以将此 LinearLayout更改为 RelativeLayout