2013-05-23 4 views
6

2つの列と2つの行を持つtableLayoutを持っていますが、行と最後の列の両方にwidthのmatch_parentがありますが、レイアウトは親の幅を満たしていません。 。ここでlayout_width = matchparentでTableLayoutが親に一致しない

はコードです:

<TableLayout android:layout_width="match_parent"> 
    <TableRow android:layout_width="match_parent"> 
     <TextView 
      android:layout_width="wrap_content" 
      android:text="static" /> 
     <EditText 
      android:layout_width="match_parent" 
      android:text="text" /> 
    </TableRow> 

    <TableRow android:layout_width="match_parent"> 
     <TextView 
      android:layout_width="wrap_content" 
      android:text="static2" /> 
     <EditText 
      android:layout_width="match_parent" 
      android:text="text2" /> 
    </TableRow> 
</TableLayout> 

私は親の幅を持つ行ごとに行う必要がありますか?

Ps:私が働く場所は私のコードを投稿することを許可しないので、コードのできるだけ近くにコードを書きます。私はそれが正しいかどうかわからない、私はテストすることはできません。

答えて

20

は、私はこのWILが、その分重量属性がうまく動作しないので、のTextViewを含む他の行よりも広くなっている他の行がある場合は、

<TableLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    > 

    <TableRow android:layout_width="match_parent" > 

     <TextView 
      android:layout_width="fill_parent" 
      android:layout_weight="1" 
      android:text="static" /> 

     <EditText 
      android:layout_width="match_parent" 
      android:layout_weight="1" 
      android:text="text" /> 
    </TableRow> 

    <TableRow android:layout_width="match_parent" > 

     <TextView 
      android:layout_width="fill_parent" 
      android:layout_weight="1" 
      android:text="static2" /> 

     <EditText 
      android:layout_width="match_parent" 
      android:layout_weight="1" 
      android:text="text2" /> 
    </TableRow> 
</TableLayout> 
+3

'android:layout_weight =" 1 "'私の 'TableRow'で私のトリックがありました。ありがとう! – Alias

1

また考えることができます考えて、このコードを試してみてください。この例を考える:

<?xml version="1.0" encoding="utf-8"?> 
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/diseno" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

    <TableRow 
     android:id="@+id/tableRow1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

      <TextView 
       android:id="@+id/nombre" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/nombre" /> 

       <EditText 
        android:id="@+id/campo1" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:inputType="text" /> 

    </TableRow> 

    <TableRow 
     android:id="@+id/tableRow2" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

     <TextView 
      android:id="@+id/apellidos" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/apellidos" 
      android:layout_weight="1"/> 

     <EditText 
      android:id="@+id/campo2" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:inputType="text" 
      android:layout_weight="1" /> 

    </TableRow> 

    <TableRow 
     android:id="@+id/tableRow3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 

     <CheckBox 
      android:id="@+id/tick" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:checked="false" 
      android:text="@string/¿Quieres suscribirte a la página?"/> 

    </TableRow> 

したがって、これは結果である:

  • 3番目の列のセルは、すべて左のセルの幅を設定しています。
  • 最初の行EditTextは、左のセルが終わるところから始まります(画面幅のほぼ4分の3)。
  • 2番目の行には各セルに1の重みが与えられているため、行の両方のセルは画面の半分を測定する必要がありますが、最初のセルは幅が広いため、重みは比例で計算されます。右のセルでは、最初のセルが3番目のセルの内容より小さくならないため、画面の半分になることはありません。