0

EditTextフィールドに、ピクセル数を明示的に指定せずにレイアウト内の適切な量のスペースを占有させることができないことに混乱し、不満を感じています。

明らかな何かが欠けていることは確かですが、私の経験では、EditTextはlayout_weightを完全に無視し、layout_weightを "wrap_content"にすると、入力されたテキストに合わせて動的に拡大/縮小します。 fill_parentの重みを指定すると、親レイアウトのスペース。

つまり...親レイアウトの一部(私の場合は線形ですが、柔軟性があります)を占めるEditTextフィールドを使用して、その横にラベルを付けて次のようにするための正しいパスは何ですか?

Name: [ EDIT TEXT HERE   ]
Phone:[ EDIT TEXT HERE   ]

TIA

4

4 に答える 4

2

いくつかの異なることができます。前述のように、レイアウトにはピクセルではなく dp を使用する必要があります。dp を使用すると、ビューを解像度ではなく画面の物理サイズでスケーリングできます。

各ラベルの右側に表示され、画面の残りの部分を占めるように編集ボックスを指定する例を次に示します。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/name_label"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:text="Name:" />
    <TextView
        android:id="@+id/phone_label"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:layout_below="@id/name_label"
        android:text="Phone:" />
    <EditText
        android:id="@+id/name_text"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_toRightOf="@id/name_label" />
    <EditText
        android:id="@+id/phone_text"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_toRightOf="@id/phone_label"
        android:layout_below="@id/name_text" />

</RelativeLayout>

重みが使用されている LinearLayout の例を次に示します。

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp" 
        android:orientation="horizontal"> 
        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent" 
            android:text="Name:" 
            android:layout_weight="1"/>
        <EditText
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="5"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp" 
        android:orientation="horizontal"> 
        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent" 
            android:text="Phone:" 
            android:layout_weight="1"/>
        <EditText
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="5"/>
    </LinearLayout>

</LinearLayout>

LinearLayout には 7 つのビューがあり、RelativeLayout には 5 つのビューで同様のことが行われていることに注意してください。LinearLayouts は便利ですが、より複雑です。レイアウトが複雑になると、特にネストした場合に、RelativeLayouts よりもパフォーマンスが低下します。

于 2012-05-25T21:38:16.523 に答える
1

各行に水平を使用しLinearLayoutます。

その中に、水平線を追加しLinearLayoutて「ラップ」しTextViewます。LinearLayout20のaを与えますlayout_weight(例えば)。

別の水平線を使用LinearLayoutしてを「ラップ」し、EditTextに設定EditTextfill_parentますが、その外側LinearLayoutのalayout_weightを80(または、私が言っていることを理解している場合は、20 + 80 = 100%に基づく任意の値)にします。

編集:また、複数行が必要な場合は、レイアウトファイル全体を簡略化するために、「単一行」レイアウトファイルを定義して、カスタムレイアウトエントリとして使用できます。

于 2012-05-25T21:31:51.257 に答える
0

で作業する必要がありLayoutます。LinearLayoutあなたの目的に適したレイアウトではありません。をTableLayoutご覧ください。あなたの要件を満たしていると思います。TableLayoutチュートリアルをご覧ください。

于 2012-05-25T21:28:07.200 に答える
0

// edittext の最小幅と最大長を設定します

android:minWidth="40"
android:maxLength="30"
android:layout_width="wrap_content"

常に最小幅が表示され、文字が 30 を超えないようにします。

于 2012-05-25T21:19:50.103 に答える