1

顧客情報を取得するためのアクティビティがあります。1 行目は名前、2 行目は番地、3 行目は都市、州、郵便番号です。RelativeLayout を使用し、android:layout_below と android:layout_toRightOf を使用して目的を達成できますが、ビューの幅を具体的に指定する必要があります。すべてのサイズが異なる Android の古いバージョンと新しいバージョンの間でユーザーが向きを変えたり切り替えたりすると、その幅は奇妙に見えるかもしれません。LinearLayout のウェイト機能を利用したいのですが、すべてが 1 行に表示されます。LinearLayout 内にビューの行を作成するために改行を挿入するにはどうすればよいですか?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/custLabel"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:layout_width="0dip"  
android:text="Name"/>
<EditText
android:id="@+id/customer"
android:layout_height="wrap_content"
android:layout_weight=".75"
android:layout_width="0dip"/>
<TextView
android:id="@+id/addressLabel"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:layout_width="0dip"
android:text="Address"/>
<EditText
android:id="@+id/address"
android:layout_height="wrap_content"
android:layout_weight=".75"
android:layout_width="0dip"/>
<TextView
android:id="@+id/cityLabel"
android:layout_height="wrap_content"
android:layout_weight=".1"
android:layout_width="0dip"
android:text="City"/>
<EditText
android:id="@+id/city"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:layout_width="0dip"/>
<TextView
android:id="@+id/stateLabel"
android:layout_height="wrap_content"
android:layout_weight=".1"
android:layout_width="0dip"
android:text="State"/>
<EditText
android:id="@+id/state"
android:layout_height="wrap_content"
android:layout_weight=".20"
android:layout_width="0dip"/>
<TextView
android:id="@+id/zipLabel"
android:layout_height="wrap_content"
android:layout_weight=".1"
android:layout_width="0dip"
android:text="Zip"/>
<EditText
android:id="@+id/zip"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:layout_width="0dip"/>
</LinearLayout>
4

3 に答える 3

4

これを行うには、ネストされた を使用する必要がありますLinearLayout。私はこのようなことを考えます

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/custLabel"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight=".25"
            android:text="Name" />

        <EditText
            android:id="@+id/customer"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight=".75" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/addressLabel"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight=".25"
            android:text="Address" />

        <EditText
            android:id="@+id/address"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight=".75" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/cityLabel"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight=".1"
            android:text="City" />

        <EditText
            android:id="@+id/city"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight=".25" />

        <TextView
            android:id="@+id/stateLabel"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight=".1"
            android:text="State" />

        <EditText
            android:id="@+id/state"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight=".20" />

        <TextView
            android:id="@+id/zipLabel"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight=".1"
            android:text="Zip" />

        <EditText
            android:id="@+id/zip"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight=".20" />
    </LinearLayout>
</LinearLayout>

動作するはずです。

于 2012-02-06T14:29:29.763 に答える
3

線形ビューのプロパティは、子ビューを水平または垂直に配置することです。各TextViewとEditテキストを1行おきに並べ替えることはできません。そのためには、テーブルレイアウトや、現在の線形レイアウト内の別の線形レイアウトなどの追加のレイアウトを使用する必要があります。

次に例を示します。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TableLayout
    android:id="@+id/tableLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight=".25" >

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

        <TextView
            android:id="@+id/custLabel"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".25"
            android:text="Name" />

        <EditText
            android:id="@+id/customer"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".75" >

            <requestFocus />

        </EditText>
    </TableRow>

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

        <TextView
            android:id="@+id/addressLabel"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".25"
            android:text="Address" />

        <EditText
            android:id="@+id/address"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".75" />
    </TableRow>

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

        <TextView
            android:id="@+id/cityLabel"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".1"
            android:text="City" />

        <EditText
            android:id="@+id/city"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".25" />
    </TableRow>

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

        <TextView
            android:id="@+id/stateLabel"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".1"
            android:text="State" />

        <EditText
            android:id="@+id/state"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".20" />

    </TableRow>

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

        <TextView
            android:id="@+id/zipLabel"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".1"
            android:text="Zip" />

        <EditText
            android:id="@+id/zip"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".25" />
    </TableRow>

</TableLayout>

于 2012-02-06T14:42:50.627 に答える
1

あなたがしたいことは、単一の LinearLayout では不可能です。TableLayoutをチェックしてください。

于 2012-02-06T14:26:35.913 に答える