在RelativeLayout中使用Android ScrollView和ButtonBar

8

我正在尝试实现一个登录视图,该视图显示了几个EditText和一个标志,以及底部的ButtonBar,类似于这样:

alt text 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 中,我很难解决这个问题。

2个回答

15

事实证明,解决问题需要两个步骤:

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

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


0
如果您的用例支持在横向方向隐藏按钮栏,您可以检查Resources.getSystem().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE并将按钮栏设置为View.GONE
您还可能需要在清单文件中的<activity>上设置android:windowSoftInputMode="adjustResize"。当根布局是ScrollView时,Android才会自动将其放入adjustResize(如果我没记错的话)。

很不幸,我需要在横屏模式下保持按钮栏的可见性,因为你需要该栏来提交表单(而且我不希望使用G1或其他设备的用户必须关闭键盘并旋转手机才能提交)。好消息是,你提到的android:windowSoftInputMode="adjustResize"建议让我找对了方向,我已经解决了问题。我马上会发布我的答案。感谢你的帮助! - russell_h

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接