2010-12-01 10 views
24

inside the cellのテキストが囲まれていますが、when the text cell is largeに合わせてTableLayoutを取得できません。Androidテーブルレイアウトの書式設定に問題があります

これは私のTableLayoutです。単純な2行2列のテーブル。左の列のセルはmultilineです。これらのセルの1つのテキストが2行で分割するのに十分な大きさである場合、列は画面全体を埋めるように引き伸ばされ、右側の列は画面から押し出されます。

なぜですか?どのようにしてテーブル全体を強制的に画面内にとどめることができますか? ヒントは歓迎されます。

マイTableLayoutは以下の通りです:レイアウトを表示するには

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/myTableLayout" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
> 
<TableRow> 
    <TextView 
     android:text="Account 4 with a very large name to see if it breaks on two or more lines" 
     android:singleLine="false" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     > 
    </TextView> 
    <TextView 
     android:text="$400.00" 
     android:gravity="right" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     > 
    </TextView> 
</TableRow> 

<TableRow> 
    <TextView 
     android:text="Account 5" 
     android:singleLine="false" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     > 
    </TextView> 
    <TextView 
     android:text="$400.00" 
     android:gravity="right" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     > 
    </TextView> 
</TableRow> 

</TableLayout> 

コードは基本的なものです:

public class AccountBrowserTest 
    extends Activity 
{ 
    public void onCreate(Bundle savedInstanceState) 
    {  
     super.onCreate(savedInstanceState); 

     this.setContentView(R.layout.accountbrowser_test); 

    } 
} 

答えて

32

はアンドロイドを追加

<TableLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:stretchColumns="1" 
    android:shrinkColumns="1"> 

1値を使用すると、変更が発生する列です。あなたは指定することができ、より、このような一つの値:

android:stretchColumns="0,1" 

か、変更は、このようにすべてのあなたの列に起こるしたい場合:

android:stretchColumns="*" 

詳細についてはこちらをご覧ください:http://android-pro.blogspot.com/2010/02/table-layout.html

0

それはあなたの複数行のセルの幅のためにwrap_contentとしなければならないかもしれません。

さらに明示的な幅を指定する方法、つまりwidth to 0を設定して、すべてのセルにlayout_gravity = 1を指定する方法があります。これにより、各列が同じ幅の幅を占めるようにする必要があります(または、ある列の幅を他の列よりも大きくしたい場合は調整できます)。

0

免責事項:私はレイアウトで本当に良いではないので、私はいくつかの値を持つビットの周りに演奏し、これは動作するように見えた(私はあなたの良い説明を与えることができない理由が):

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/myTableLayout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <TableRow> 
     <TextView 
      android:text="Account 4 with a very large name to see if it breaks on two or more lines" 
      android:singleLine="false" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
     /> 
     <TextView 
      android:text="$400.00" 
      android:gravity="right" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.001" 
     /> 
    </TableRow> 

    <TableRow> 
     <TextView 
      android:text="Account 5" 
      android:singleLine="false" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
     /> 
     <TextView 
      android:text="$400.00" 
      android:gravity="right" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.001" 
     /> 
    </TableRow> 
</TableLayout> 

また、私はアクティビティでこのレイアウトを試していないことに注意してください。XMLエディタのレイアウトビューでのみ見ましたが、少なくともそこではうまくいきました。

0

また、TableLayoutでstretchColumnsおよび/またはshrinkColumnsを操作することもできます。

8

私は私のアプリで同じ問題を抱えていたし、次の解決策が見つかりました:これは、2列で正常に動作します

<TableLayout 
    android:shrinkColumns="0" 
    android:stretchColumns="1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 
</TableLayout> 

を。

5

私は同じ問題を抱えていたとTableLayout要素に

android:stretchColumns="0" 

を追加し、長すぎたのTextViewに

android:layout_width="0dp" 

を設定することで、それを解決しました。

+2

とまさにこれが意味を拡張..あなたを助けますか?これが解決策であることはどうでしたか? – Paul

1

一時的な修正がありますが、実行します。ここで

android:stretchColumns = " (number of actual col) - 1 " 

ませソリューションは、私の場合で働いていません。 TableLayout要素にstretchColumnsandroid:shrinkColumns

5

テーブルレイアウト内では、フィッティングのために縮小することを許可する列のshrinkColumnsプロパティを使用します。

最も簡単な方法は次のとおりです。

<TableLayout 
android:shrinkColumns="*"/> 

すべての列を縮小するか、あなたは、あなたのケースで列インデックスはゼロからのスタートということ

<TableLayout 
android:shrinkColumns="0,1"/> 

ノートされる、「*」あなたが縮小する列のインデックスを作成する代わりにすることができます最新のコードでは、最初の(index = 0)と2番目の(index = 1)の列を縮小することができます。

0

・ホープ、このコードは パブリッククラスMainActivityアクティビティ{

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
} 

<TextView 
     android:id="@+id/tv1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:padding="8dp" 
     android:singleLine="false" 
     android:text="Account 4 with a very large \n name to see if it breaks \n on two or more lines" > 
    </TextView> 

    <TextView 
     android:id="@+id/tv2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:padding="8dp" 
     android:text="$400.00" > 
    </TextView> 
</TableRow>