1

我會在Linearlayout裏放置兩個表格佈局。但用我的代碼,我只能看到一個表。這是代碼:linearLayout裏面的兩個tableLayout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context=".MainActivity" > 

<TableLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center" 
    android:stretchColumns="1" > 

    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:gravity="center" > 

     <TextView 
      android:id="@+text_view/tw_nome" 
      android:text="@string/tw_nome" /> 

     <EditText 
      android:id="@+edit_text/et_nome" 
      android:hint="@string/et_nome" 
      android:imeOptions="actionNext" 
      android:singleLine="true" /> 
    </TableRow> 

    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:gravity="center" > 

     <TextView 
      android:id="@+text_view/tw_cognome" 
      android:text="@string/tw_cognome" /> 

     <EditText 
      android:id="@+edit_text/et_cognome" 
      android:hint="@string/et_cognome" 
      android:imeOptions="actionDone" 
      android:singleLine="true" /> 
    </TableRow> 
</TableLayout> 

<TableLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center" > 

    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:gravity="center" > 

     <TextView 
      android:id="@+text_view/tw_sesso" 
      android:text="@string/tw_sesso" /> 

     <RadioGroup 
      android:id="@+radio_group/rg" 
      android:orientation="horizontal" > 

      <RadioButton 
       android:id="@+radio_button/button_m" 
       android:text="@string/button_m" /> 

      <RadioButton 
       android:id="@+radio_button/button_f" 
       android:text="@string/button_f" /> 
     </RadioGroup> 
    </TableRow> 

    <TableRow 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="center" > 

     <Button 
      android:id="@+button/button_salva" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/button_salva" /> 

     <Button 
      android:id="@+button/button_annulla" 
      android:text="@string/button_annulla" /> 
    </TableRow> 
</TableLayout> 

有什麼不對?

回答

2

這兩個表格佈局的寬度和高度定義爲match_parent,所以他們採取與父母相同的空間。父母沒有足夠的空間讓兩個孩子顯示與父母相同的尺寸。

你將如何解決這個問題取決於父LinearLayout的方向。

4

並排還是垂直?

如果您正在尋找並排,請嘗試layout_weight。還請注意layout_width中的更改。

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

    <TableLayout 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:stretchColumns="1" > 
      <!-- rows --> 
    </TableLayout> 

    <TableLayout 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:stretchColumns="1" > 
      <!-- rows --> 
    </TableLayout> 
</LinearLayout> 

更新從有人問爲什麼layout_width應設置爲0dp的意見。

設置0dp是使用layout_weight時的優化。設置寬度爲0dp告訴佈局忽略layout_height並使用layout_weight代替,在這種情況下給每個TableLayout均勻的寬度。

其實我問這個同樣的問題,得到了一些有用的答案,如果它是有幫助的Why is 0dp considered a performance enhancement?

+0

*這是,如果你的LinearLayout中的方向是水平的。如果您的方向是垂直的,請切換寬度/高度指令。 – 2013-02-18 21:57:35

+0

我可以問你爲什麼寬度或高度的「0dp」? – giozh 2013-02-19 09:17:03

+0

我知道這聽起來很尷尬,但當您使用'layout_weight'時,設置'0dp'是一種優化。將寬度設置爲'0dp'告訴佈局忽略'layout_height'並使用'layout_weight'。 – Kirk 2013-02-19 14:20:47

1

放的LinearLayout裏面:

<ScrollView/> </ScrollView> 
相關問題