8

我正在尝试实现一个登录视图,其中几个 EditTexts 和一个徽标显示在屏幕上,底部有一个 ButtonBar,如下所示:

替代文字 http://russellhaering.com/media/addAccount.png

问题是在非常小的屏幕上,特别是当它们横向旋转时,整个主视图不适合屏幕。

我目前有

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#234C59"  >
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:paddingTop="5dip"
        android:paddingLeft="15dip"
        android:paddingRight="15dip"
        android:orientation="vertical" >
        <ImageView
            android:id="@+id/logo"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:scaleType="fitCenter"
            android:layout_centerHorizontal="true"
            android:src="@drawable/logo" />
        <EditText
            android:id="@+id/input_email"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dip"
            android:inputType="textEmailAddress"
            android:singleLine="true"
            android:hint="Username or email" />
        <EditText
            android:id="@+id/input_password"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop=""
            android:inputType="textPassword"
            android:singleLine="true"
            android:hint="Password" />
    </LinearLayout>
    <LinearLayout
        android:id="@+id/buttonbar_login"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal"
        style="@android:style/ButtonBar" >
        <Button
            android:id="@+id/button_signup"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Sign Up" />
        <Button
            android:id="@+id/button_login"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Log In" />
    </LinearLayout>
</RelativeLayout>

我尝试将第一个 LinnearLayout 封装在 ScrollView 中,如下所示:

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
            <!-- Linear Layout Here -->
</ScrollView>

但这会带来两个问题:

  1. ScrollView 不滚动,即使在数据不完全适合的屏幕上也是如此

  2. ButtonBar 浮动在屏幕键盘的顶部,当键盘出现时会遮挡更多的屏幕。

当我在 ScrollView 中有按钮时,一切都很好,但是现在我在 ButtonBar 中有它们,我很难弄清楚这一点。

4

2 回答 2

15

事实证明,解决方案需要两个步骤:

  1. 无法滚动是因为 ScrollView 在 Button Bar 后面。为了解决这个问题,我在 Button Bar 下方定义了 ScrollView,然后用来android:layout_above="@id/buttonbar_login"强制 ScrollView 完全位于 Button Bar 上方。

  2. 显然,当屏幕键盘打开时,如果您有 ScrollView,它将调整大小,允许 Button Bar 随键盘浮动。为了解决这个问题,我修改了清单并添加了 android:windowSoftInputMode="adjustPan" 以防止 ScrollView 调整大小。

于 2010-08-03T05:36:25.647 回答
0

如果您的用例支持横向隐藏按钮栏,您可以检查Resources.getSystem().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE并将按钮栏设置为View.GONE.

您可能还需要在清单文件中android:windowSoftInputMode="adjustResize"设置<activity>adjustResize只有当根布局为ScrollView(iirc)时, Android 才会自动将其放入。

于 2010-08-03T03:57:05.010 回答