2

次の1つの列に表示したいフォームがあります。

     account name
______________________

       password
______________________

[       connect       ]

水平方向のスペースが限られている場合(つまり、縦向き)。そして、水平方向のスペースが豊富な場合は、2列の向きに流したいと思います。

account name _________________
    password _________________
[          connect            ]      

レイアウトリソースのペアでこれを実行できることは知っていますが、動的な列数を持つレイアウトを1つだけにしたいのです。

4

1 に答える 1

5

これは有用な例ではないかもしれませんが、このアプローチは有用であることがわかるかもしれません。

まず、GridLayout

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="16dp"
        android:useDefaultMargins="true"
        android:columnCount="@integer/create_account_pane_column_count"
        android:alignmentMode="alignBounds" >

<!-- Error messages -->
<TextView
        android:id="@+id/errors"
        android:layout_columnSpan="@integer/create_account_pane_column_count"
        android:textColor="@color/error_red"
        android:textSize="@dimen/small_text_size" />

<TextView
        android:id="@+id/account_name_label"
        android:layout_gravity="fill_horizontal"
        android:text="@string/label_account_name"
        android:textColor="?android:textColorSecondary" />

<EditText
        android:id="@+id/account_name"
        android:layout_gravity="fill_horizontal"
        android:ems="10"
        android:hint="@string/hint_account_name"
        android:inputType="textNoSuggestions" >

    <requestFocus />
</EditText>

<TextView
        android:id="@+id/password_label"
        android:layout_gravity="fill_horizontal"
        android:text="@string/label_password"
        android:textColor="?android:textColorSecondary" />

<EditText
        android:id="@+id/password"
        android:layout_gravity="fill_horizontal"
        android:ems="10"
        android:hint="@string/hint_password"
        android:inputType="textPassword" />

<Button
        android:id="@+id/connect"
        android:layout_columnSpan="@integer/create_account_pane_column_count"
        android:layout_gravity="fill_horizontal"
        android:text="@string/connect" />

<Button style="?android:borderlessButtonStyle"
        android:id="@+id/create_account"
        android:layout_columnSpan="@integer/create_account_pane_column_count"
        android:layout_gravity="center_horizontal"
        android:text="@string/create_a_new_account"
        android:textColor="@color/kas_yellow" />

</GridLayout>

次に、2つの整数リソースファイル、values-large-land-v11/integers.xmlhas<integer name="create_account_pane_column_count">2</integer>values/integers.xmlhasがあります<integer name="create_account_pane_column_count">1</integer>。これにより、動的な列が作成されます。デバイスと向きがラージランドv11(7 "タブレット以上の横向き)と一致しない限り、1つの列があります。

最後に、ラベルを適切に配置する(つまり、スタックする場合は中央に配置し、インラインの場合は右揃えにする)ために、xml値がリソース(整数でも文字列でもない)を受け入れていないように見えるため、プログラムで設定しました。

@Override
public void onConfigurationChanged(Configuration new_configuration) {
    super.onConfigurationChanged(new_configuration);

    alignLabels();
}

/**
 * Aligns the labels depending on the number of columns in the layout
 */
private void alignLabels() {
    final int column_count = getResources().getInteger(R.integer.create_account_pane_column_count);
    final int gravity = column_count == 1? Gravity.CENTER_HORIZONTAL : Gravity.RIGHT;
    _account_name_label.setGravity(gravity);
    _password_label.setGravity(gravity);
}

alignLabels()また、onCreateまたはonCreateView( sの場合)を呼び出しFragmentて、初期ページレイアウトの重力を設定します。

補足:ラベルに気付いた場合、関連するフィールドにフォーカスがある場合、ラベルはアクセントカラーを使用します。

横向き 縦向き

于 2012-11-07T04:14:14.723 に答える