2012-12-11 72 views
4

TableLayout在每一行上都有一個TextViewEditText。當EditText具有多行,TableRow包裝,但是當TextEdit,它不..TableRow不包裝TextView

Simplyfied佈局:

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

    <TableRow> 

     <TextView 
      android:id="@+id/textView1" 
      android:text="Lorem ipsum dolor sit amet, consectetur" /> 

     <EditText 
      android:id="@+id/textView2" 
      android:text="Foo" /> 
    </TableRow> 

    <TableRow> 

     <TextView 
      android:id="@+id/textView3" 
      android:text="Foo" /> 

     <EditText 
      android:id="@+id/textView4" 
      android:text="Lorem ipsum dolor sit amet, consectetur" /> 
    </TableRow> 

</TableLayout> 

呈現:Render 1

但是,當這兩個控件都TextEdit,它呈現正確。

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

    <TableRow> 

     <TextView 
      android:id="@+id/textView1" 
      android:text="Lorem ipsum dolor sit amet, consectetur" /> 

     <TextView 
      android:id="@+id/textView2" 
      android:text="Foo" /> 
    </TableRow> 

    <TableRow> 

     <TextView 
      android:id="@+id/textView3" 
      android:text="Foo" /> 

     <TextView 
      android:id="@+id/textView4" 
      android:text="Lorem ipsum dolor sit amet, consectetur" /> 
    </TableRow> 

</TableLayout> 

呈現爲:Render 2

我應該爲Android做才能正確地呈現先佈局?

PS:我已經嘗試添加android:layout_width="fill_parent"android:layout_height="wrap_content「屬性TableRowTextViewEditText控制,一切都沒有改變

回答

2
<?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="vertical" > 

    <TableLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" > 

     <TableRow 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" 
      android:weightSum="4" > 

      <TextView 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginRight="1dp" 
       android:layout_weight="1" 
       android:background="#B0B0B0" /> 

      <TextView 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginRight="1dp" 
       android:layout_weight="1" 
       android:background="#B0B0B0" /> 

      <TextView 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginRight="1dp" 
       android:layout_weight="1" 
       android:background="#B0B0B0" /> 

      <TextView 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:background="#B0B0B0" /> 
     </TableRow> 

     <TableRow 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="1dp" 
      android:orientation="vertical" 
      android:weightSum="4" > 

      <TextView 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginRight="1dp" 
       android:layout_weight="1" 
       android:background="#B0B0B0" /> 

      <TextView 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginRight="1dp" 
       android:layout_weight="1" 
       android:background="#B0B0B0" /> 

      <TextView 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginRight="1dp" 
       android:layout_weight="1" 
       android:background="#B0B0B0" /> 

      <TextView 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:background="#B0B0B0" /> 
     </TableRow> 

     <TableRow 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="1dp" 
      android:orientation="vertical" 
      android:weightSum="4" > 

      <TextView 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginRight="1dp" 
       android:layout_weight="1" 
       android:background="#B0B0B0" /> 

      <TextView 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginRight="1dp" 
       android:layout_weight="1" 
       android:background="#B0B0B0" /> 

      <TextView 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginRight="1dp" 
       android:layout_weight="1" 
       android:background="#B0B0B0" /> 

      <TextView 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:background="#B0B0B0" /> 
     </TableRow> 

     <TableRow 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="1dp" 
      android:orientation="vertical" 
      android:weightSum="4" > 

      <TextView 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginRight="1dp" 
       android:layout_weight="1" 
       android:background="#B0B0B0" /> 

      <TextView 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginRight="1dp" 
       android:layout_weight="1" 
       android:background="#B0B0B0" /> 

      <TextView 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginRight="1dp" 
       android:layout_weight="1" 
       android:background="#B0B0B0" /> 

      <TextView 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:background="#B0B0B0" /> 
     </TableRow> 

     <TableRow 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="1dp" 
      android:orientation="vertical" 
      android:weightSum="4" > 

      <TextView 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginRight="1dp" 
       android:layout_weight="1" 
       android:background="#B0B0B0" /> 

      <TextView 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginRight="1dp" 
       android:layout_weight="1" 
       android:background="#B0B0B0" /> 

      <TextView 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginRight="1dp" 
       android:layout_weight="1" 
       android:background="#B0B0B0" /> 

      <TextView 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:background="#B0B0B0" /> 
     </TableRow> 

     <TableRow 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="1dp" 
      android:orientation="vertical" 
      android:weightSum="4" > 

      <TextView 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginRight="1dp" 
       android:layout_weight="1" 
       android:background="#B0B0B0" /> 

      <TextView 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginRight="1dp" 
       android:layout_weight="1" 
       android:background="#B0B0B0" /> 

      <TextView 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginRight="1dp" 
       android:layout_weight="1" 
       android:background="#B0B0B0" /> 

      <TextView 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:background="#B0B0B0" /> 
     </TableRow> 
    </TableLayout> 

</LinearLayout> 

這些灰色單元R文本框

enter image description here

+0

那麼這個答案和這個問題之間的關係是什麼? – hkutluay

+0

只需在其中輸入文本值,你會發現如何呈現表格中的文本 –

+0

正如我提到的問題,當兩個控件都是TextEdit時,它正確渲染。 – hkutluay

1

到TableLayout嘗試添加

android:stretchColumns="*" 

,如果達太犯規。工作嘗試添加重量

   <TableRow 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="2" > 


And to the TextView and EditText add 
       android:layout_weight="1"   
+0

不工作。仍然有同樣的問題。 – hkutluay

0

添加android:layout_gravity="center_vertical"TextView或將android:background="#B0B0B0"添加至EditText強制android正確呈現。

4

我有同樣的問題,什麼解決一切的權重設置爲我想換行的textviews。我的兩美分...