30

編集ボックスの最大幅を設定したい。だから私はこの小さなレイアウトを作成しました:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:maxWidth="150dp" > 

    <EditText 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:ems="10" /> 
</LinearLayout> 

とにかく、ボックスは150 dpを超える可能性があります。android:maxWidth="150dp"EditText同じ結果が得られます。最大幅と最小幅の両方を同じサイズに設定すると解決するはずです(maxWidth は fill_parent では機能しません)。

<EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:maxWidth="50dp"
        android:minWidth="50dp" />

しかし、そうではありません。

ここで私の問題の解決策を見つけました: ViewGroup に最大幅を設定する と、これはうまく機能します。しかし、私maxWidthは理由のために属性がここにあると思います。どのように使用する必要がありますか?または、これに関するドキュメントはありますか?私はこの問題に何時間も費やしましたが、この単純な属性の使用方法をまだ理解していません。

4

2 に答える 2

36

編集ボックスの最大幅を設定したい。

あなたの例では:

<EditText 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:ems="10" /> 

および属性が同じ値を設定しようとしていますlayout_widthemsAndroid は大きい方のfill_parent値を選択するようで、他の値は無視します。そして、これを使用すると:

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ems="10"
    android:maxWidth="50dp"
    android:minWidth="50dp" />

ここでemsmaxWidthは同じ値を設定しようとしていますが、ここでも大きい方の値が使用されます。したがって、実際に必要なものに応じて、次を使用できます。

<EditText 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:ems="10" /> 

android:ems="10"または、dp を使用する場合はに置き換えandroid:maxWidth="50dp"ます。

最後に、LinearLayout には EditText という 1 つの子しかありません。通常、これが発生した場合は、LinearLayout タグを削除して、EditText を単独で使用できます。

于 2012-12-06T20:00:36.407 に答える