2012-05-28 110 views
1

我創建了一個帶有兩行的TableLayout,其中第一行有一個TextView,第二行有兩列,第一行有一個ListView,第二行有一個EditTextButton,每行在不同的行中。我希望兩行的高度相同(正好是屏幕的一半),第二行的兩列的寬度應該相等(正好是屏幕的一半)。我想建立這樣的事情:enter image description here如何自定義佈局(高度和寬度)以及佈局的容器佈局?

我應該如何繼續?我應該選擇哪種佈局?

回答

3

我不認爲你當前的解決方案將工作(因爲需要相等的空間(寬度和高度)+ ListView存在)。一種解決方案是使用嵌套weights(這是不好的表現,但(可能是,不知道你做了什麼),將打破你的應用程序不是那麼重要),如在下面的佈局:

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

    <include 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     layout="@layout/identical_layout" /> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:text="TextView" /> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" > 

     <ListView 
      android:id="@+id/list" 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" > 
     </ListView> 

     <LinearLayout 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:orientation="vertical" > 

      <EditText 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" /> 

      <Button 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="ddd" /> 
     </LinearLayout> 
    </LinearLayout> 




</LinearLayout> 

另一種選擇避免嵌套weights問題是使用RelativeLayout像下面(I沒有測試它):

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

    <include 
     android:id="@+id/included_layout" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     layout="@layout/identical_layout" /> 

    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_below="@id/included_layout" > 

     <View 
      android:id="@+id/anchor" 
      android:layout_width="0dp" 
      android:layout_height="0dp" 
      android:layout_centerVertical="true" /> 

     <TextView 
      android:id="@+id/textView1" 
      android:layout_width="fill_parent" 
      android:layout_height="0dp" 
      android:layout_above="@id/anchor" 
      android:layout_alignParentTop="true" 
      android:background="#99cc00" 
      android:text="TextView" /> 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="0dp" 
      android:layout_alignParentBottom="true" 
      android:layout_below="@id/anchor" > 

      <ListView 
       android:id="@+id/list" 
       android:layout_width="0dp" 
       android:layout_height="fill_parent" 
       android:layout_weight="1" > 
      </ListView> 

      <LinearLayout 
       android:layout_width="0dp" 
       android:layout_height="fill_parent" 
       android:layout_weight="1" 
       android:orientation="vertical" > 

       <EditText 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" /> 

       <Button 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="ddd" /> 
      </LinearLayout> 
     </LinearLayout> 
    </RelativeLayout> 

</RelativeLayout> 

identical_layout佈局文件表示由您Activities共享的公共佈局。

+0

+1000謝謝你真的很棒,目前我正在試圖用TableLayout來實現這個目標,但是你的回答讓我很容易,我想實現第一個選項......如果他們是一些性能開銷,我會肯定會去第二種方法...再次感謝... – aProgrammer

+0

那工作...現在唯一的問題是ListView不滾動,我該怎麼做?再次感謝十億... – aProgrammer

+0

@anDroider我不知道爲什麼你的'ListView'不滾動。有一些與你的代碼相關的東西,而不是上面的佈局文件,因爲我現在測試它們,而'ListView'按預期工作。 – Luksprog