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> 
相關問題