0

私のアプリケーションでは、TableView をプログラムで作成しています。テーブルは、通常、5 つのテキストビューを持つ次の行にまたがる長いテキスト (画面幅の約 80%) を含むテキストビューを持つ一連の単一行で構成されます。これはうまくいきましたが、残念ながら、これらの行に1つのテキストボックスでチェックボックスも配置する必要があります. テキストボックスのコンテンツが長いため、このチェックボックスは右側に配置し、可能な限り幅を狭くする必要があります。ただし、テキスト ボックスを 4 列のみにし、5 列目に予約されたスペースにチェックボックスを配置すると、チェックボックスの幅が広すぎて、テキストには十分ではありません (折り返されてしまいます)。この状況を説明する例を用意しました。

+---------------------------------------------------+
|TextView spanning over 5 columns below             |   <- 1 column spans over 5
|-----+------+---------------+------+---------------|
|col1 | col2 |Column number 3| Col4 | Column number5|   <- 5 columns
|-----+------+---------------+------+---------------|
|This spans over 4 column - not enou|[V]            |
|gh                                 |               |   <- 2 columns, 1st spans over 4
|-----+------+---------------+------+---------------|
|col1 | col2 |Column number 3| Col4 | Column number5|   <- 5 columns
|-----+------+---------------+------+---------------|
|This is what I need-checkbox maximum to right  |[V]|   <- 2 columns
+---------------------------------------------------+

最後の行は、どのように表示する必要があるかを示しています。HTML では、行内に別のテーブルをネストし、それに応じてこの新しいテーブル内で幅を分割しますが、このシナリオを Android TableLayout で構築することは不可能のようです。

これを達成する方法を知っている人はいますか?

4

1 に答える 1

0

別の試み ;) 変更は、テーブル レイアウトのプロパティ、2 行目のテキスト ビュー、およびチェック ボックスの layout_gravity にあります。この要素を使用すると、プログラムで同じことができると思います;)

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:stretchColumns="0,1,2,3" >

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

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="@string/app_name" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="@string/app_name" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="@string/app_name" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="@string/app_name" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="@string/app_name" />
    </TableRow>

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

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_span="4"
            android:text="@string/app_name" />

        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right" />
    </TableRow>

</TableLayout>

テーブルの android:shrinkColumns および android:stretchColumns プロパティを見てください。このプロパティは、次を使用して編集できます。

setColumnShrinkable(int columnIndex, boolean isShrinkable)
setColumnStretchable(int columnIndex, boolean isStretchable)

このメソッドを使用して動作するはずです (私はテストしていません)。

于 2012-04-05T15:07:37.330 に答える