1

我想在一个屏幕上显示三个不同的垂直部分,在我的Android布局中,每个屏幕占用屏幕的三分之一,顶部一个,中间一个,一个在底部。每个部分有一个TextView和一个ListView,并且ListViews可以滚动以便您可以看到所有项目,但整个页面不会移动。我曾尝试将每个TextViewListView放在LinearLayout中,并将每个LinearLayout的高度设置为屏幕总高度的三分之一,但第一个ListView只显示其中的所有项目,占用大部分屏幕,另一个部分被推下。我也尝试使用layout_weights,但由于某种原因,它不起作用。 (编辑:设置layout_weight="1"结束了工作,我不确定我第一次做错了什么)我该如何做这项工作,或者有更好的方法去做这件事?在Android布局中显示3个相同高度的可滚动ListViews

回答

1
<LinearLayout 
    android:layout_width="match_parent" android:layout_height="match_parent" 
    android:orientation="horizontal" > 

    <ListView android:layout_width="0dp" android:layout_weight="1" 
    android:layout_height="match_parent" android:background="#FF0000"/> 

    <ListView android:layout_width="0dp" android:layout_weight="1" 
    android:layout_height="match_parent" android:background="#00FF00"> 

    <ListView android:layout_width="0dp" android:layout_weight="1" 
    android:layout_height="match_parent" android:background="#0000FF"> 

</LinearLayout> 

这会给你三个等宽的列:使其行,更改方向垂直和交换的layout_heightlayout_width值对每个列表视图。如果您需要添加TextView,则必须在此代码中使用相同的宽度/高度/重量,并将ListViewTextView排列在内,以使其在此代码中的排列视图为RelativeLayoutLinearLayoutFrameLayout。为了最有效地做到这一点,请使用Framelayout并在列表视图上使用边距来抵消TextView。您可以通过使用TextView中的layout_gravityTextView相对于FrameLayout中的ListView

IE(交换第一 “列”):

<FrameLayout android:orientation="vertical" android:layout_width="0dp"  
    android:layout_weight="1" android:layout_height="match_parent" 
    android:background="#FF0000"> 

    <TextView android:text="Column!" android:background="#3Eff0000" 
    android:layout_height="40dp" android:layout_width="match_parent"> 

    <ListView android:layout_marginTop="48dp" android:layout_height="match_parent"  
    android:layout_width="match_parent" android:background="#8Aff0000"/> 

</FrameLayout> 
+0

谢谢你,这个工作;最初我曾尝试使用权重,但由于某种原因,它不起作用,但此方法确实有效! – ravensgo 2015-03-31 21:26:20

0

使用此:

enter image description here

<?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="match_parent" 
    android:orientation="horizontal" 
    android:weightSum="3"> 

    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:background="#ff89ff91"> 


     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:text="#1" 
      android:id="@+id/textView" 
      android:padding="5dp" 
      android:gravity="center" /> 

     <ListView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/listView" 
      android:layout_below="@+id/textView" /> 
    </RelativeLayout> 

    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:background="#ffff8177"> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:text="#2" 
      android:id="@+id/textView2" 
      android:padding="5dp" 
      android:gravity="center" /> 

     <ListView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/listView2" 
      android:layout_below="@+id/textView2" /> 
    </RelativeLayout> 

    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:background="#ffffe85d"> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:text="#3" 
      android:id="@+id/textView3" 
      android:padding="5dp" 
      android:gravity="center" /> 

     <ListView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/listView3" 
      android:layout_below="@+id/textView3" /> 
    </RelativeLayout> 
</LinearLayout>