如何使用GridLayout在Android上调整表单标签的方向

Dan*_*son 2 android android-orientation android-gridlayout

我有一个表单,我想在一列中显示:

     account name
______________________

       password
______________________

[       connect       ]
Run Code Online (Sandbox Code Playgroud)

当水平空间有限时(即纵向).当水平空间充足时,我希望它流入两列方向:

account name _________________
    password _________________
[          connect            ]      
Run Code Online (Sandbox Code Playgroud)

我知道你可以使用一对布局资源来做到这一点,但我想只有一个动态列数的布局.

Dan*_*son 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>
Run Code Online (Sandbox Code Playgroud)

然后我有两个整数资源文件,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"平板和以上横向).

最后,为了使标签很好地对齐(即在堆叠时居中并在内联时右对齐)我以编程方式设置它,因为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);
}
Run Code Online (Sandbox Code Playgroud)

还要求alignLabels()onCreateonCreateView(对于Fragment多个)来设定重力上的初始页面布局.

旁注:如果您注意到,我的标签会在相关字段具有焦点时使用我的强调色.

景观取向 纵向