20

各行に 3 つのボタンがある 2 行のテーブルがあります。ボタンがスペースを均等に埋めるようにするにはどうすればよいですか。HTML では、幅を 33% にします。

また、ランチャーと同様に、グリッド レイアウトとして 4 つの画像ボタンが一列に並んだビューを作成する方法を知っていますか。

4

4 に答える 4

41

タグに追加android:stretchColumns="*"してみてください。<TableLayout>

于 2010-02-10T15:09:37.200 に答える
17

私はついにここで本当の答えを見つけました: http://androidadvice.blogspot.com/2010/10/tablelayout-columns-equal-width.html

ここにもstackoverflowに関するより最小限の例があります: https://stackoverflow.com/a/2865558/265521

例:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent">
    <TableRow>
        <Button
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:text="Why is doing layouts on Android"
            android:layout_weight="1"
            />
        <Button
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:text="so"
            android:layout_weight="1"
            />
    </TableRow>
    <TableRow>
        <Button
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:text="damn"
            android:layout_weight="1"
            />
        <Button
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:text="frustrating?"
            android:layout_weight="1"
            />
    </TableRow>
</TableLayout>
于 2012-01-11T21:06:02.203 に答える
3

TableRow layout_widthをfill_parentに設定し、各ボタンにlayout_weightを1に設定します。

layout_weightは、パーセンテージのように機能します。すべてのアイテムが同じ数になる場合、それらは同じ割合のスペースを占有します。1つのボタンの重みが2で、別のボタンの重みが1の場合、最初のボタンは2倍のスペースを占有します。

まだ行っていない場合は、開発ガイドの一般的なレイアウトページで、レイアウトの概要を確認してください。

于 2010-02-10T17:03:19.147 に答える
0

したがって、投稿の結論として、それを行う正しい方法は次を設定することですNumberOfTheColumns

   <tablelayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:stretchColumns="NumberOfTheColumns"/>

必要な水平方向の「ピース」の数を設定できます。3x3 のテーブルがある場合:

 <TableLayout
    android:minWidth="25px"
    android:minHeight="25px"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/tableLayout1"
    android:stretchColumns="3">
    <TableRow
        android:id="@+id/tableRow1">
        <TextView
            android:text="@string/EventTitle"
            android:id="@+id/textView1"
            android:gravity="center_vertical"
            android:layout_column="0" />
        <EditText
            android:inputType="text"
            android:id="@+id/editText3"
            android:layout_weight="2"
            android:layout_span="2"
            android:layout_column="1" />
    </TableRow>
    <TableRow
        android:id="@+id/tableRow2">
        <TextView
            android:text="@string/EventDateStart"
            android:id="@+id/textView1"
            android:gravity="center_vertical"
            android:layout_column="0" />
        <EditText
            android:inputType="date"
            android:id="@+id/editText1"
            android:layout_weight="1"
            android:layout_column="1"
            android:layout_width="wrap_content" />
        <EditText
            android:inputType="time"
            android:id="@+id/editText2"
            android:layout_weight="1"
            android:layout_column="2"
            android:layout_width="match_parent" />
    </TableRow>
    <TableRow
        android:id="@+id/tableRow3">
        <TextView
            android:text="@string/EventDateEnd"
            android:id="@+id/textView1"
            android:gravity="center_vertical"
            android:layout_column="0"
            android:layout_width="match_parent" />
        <EditText
            android:inputType="date"
            android:id="@+id/editText1"
            android:layout_weight="1"
            android:layout_column="1" />
        <EditText
            android:inputType="time"
            android:id="@+id/editText2"
            android:layout_weight="1"
            android:layout_column="2" />
    </TableRow>
    <TableRow>
        <TextView
            android:text="@string/Description"
            android:id="@+id/textView1"
            android:gravity="center_vertical"
            android:layout_weight="3"
            android:layout_span="3"
            android:layout_column="0"
            android:layout_width="match_parent" />
    </TableRow>
    <TableRow>
        <EditText
            android:inputType="textMultiLine"
            android:id="@+id/editText3"
            android:layout_height="50dp"
            android:layout_weight="3"
            android:layout_span="3"
            android:layout_column="0"
            android:layout_width="match_parent" />
    </TableRow>
</TableLayout>

このサンプルでは、​​5x3 テーブルを見ることができます。テーブル全体の幅の何個が必要かを設定することで、各列の幅を設定できます...

于 2015-03-05T09:53:24.473 に答える