我有一个表单,我想在以下内容的单列中显示:
account name
______________________
password
______________________
[ connect ]
当水平空间有限时(即纵向)。当水平空间充足时,我希望它流入两列方向:
account name _________________
password _________________
[ connect ]
我知道您可以使用一对布局资源来完成此操作,但我只想拥有一个具有动态列数的布局。
最佳答案
这可能不是一个有用的示例,但该方法可能会有用。
首先,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>
然后我有两个整数资源文件,values-large-land-v11/integers.xml
有<integer name="create_account_pane_column_count">2</integer>
和values/integers.xml
有<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);
}
也可调用alignLabels()
在 onCreate
或onCreateView
(对于 Fragment
s)设置初始页面布局的重力。
旁注:如果您注意到的话,当相关字段具有焦点时,我的标签会使用我的强调色。
关于android - 如何使用 GridLayout 使表单标签适应 Android 上的方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13263031/