java - 布局底部有两个按钮的 ScrollView

标签 java android scrollview xml-layout

我想创建一个具有 ScrollView 的布局,在布局顶部的 ScrollView 内将有两个 Textview。在中心将有两个 Edittexts,在布局的底部将有两个按钮。 但所有内容都将位于主 ScrollView 下

我的要求的直观描述:

Required output

我已经完成了一些编码,可以滚动顶部内容,但将底部的按钮保留在 ScrollView 之外,这不是预期的功能。

我的布局编码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin" >

    <ScrollView
        android:id="@+id/my_scrollview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/LinearLayout01"
        android:scrollbars="horizontal" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/welcomeToStudy"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:text="Welcome to xxxxxxxxxxxxxxx!"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:textColor="#54575A"
                android:textSize="28sp" />

            <TextView
                android:id="@+id/pleaseEnter"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/welcomeToStudy"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="5dp"
                android:text="Please enter your details xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx."
                android:textSize="20sp" />

            <EditText
                android:id="@+id/emailSignUp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/pleaseEnter"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="5dp"
                android:ems="10"
                android:hint="Email"
                android:inputType="textEmailAddress"
                android:singleLine="true"
                android:textSize="24sp" >
            </EditText>

            <EditText
                android:id="@+id/passwordSignUp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/emailSignUp"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="5dp"
                android:ems="10"
                android:hint="Password"
                android:inputType="textPassword"
                android:singleLine="true"
                android:textSize="24sp" >
            </EditText>
        </RelativeLayout>
    </ScrollView>

    <LinearLayout
        android:id="@+id/LinearLayout01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="vertical" 
        android:layout_marginTop="5dp">

        <Button
            android:id="@+id/signUpBtn"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/next_button_selector"
            android:text="Sign Up"
            android:textColor="#FFFFFF"
            android:textSize="20sp" >
        </Button>

        <Button
            android:id="@+id/registerWithFb"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:background="@drawable/next_button_xhdpi_facebook"
            android:text="Sign up with Facebook"
            android:textColor="#FFFFFF"
            android:textSize="20sp" >
        </Button>
    </LinearLayout>

</RelativeLayout>

我应该怎样做才能获得预期的输出?

最佳答案

如果我没听懂的话,您需要在密码 EditTextButton 之间有一个空格。如果是这样,那么只需添加一个虚拟 View 即可,如下所示:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/my_scrollview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="horizontal" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/welcomeToStudy"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Welcome to xxxxxxxxxxxxxxx!"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="#54575A"
            android:textSize="28sp" />

        <TextView
            android:id="@+id/pleaseEnter"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:text="Please enter your details xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx."
            android:textSize="20sp" />

        <EditText
            android:id="@+id/emailSignUp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:ems="10"
            android:hint="Email"
            android:inputType="textEmailAddress"
            android:singleLine="true"
            android:textSize="24sp" >
        </EditText>

        <EditText
            android:id="@+id/passwordSignUp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:ems="10"
            android:hint="Password"
            android:inputType="textPassword"
            android:singleLine="true"
            android:textSize="24sp" >
        </EditText>

        <FrameLayout
            android:id="@+id/whiteSpace"
            android:layout_width="match_parent"
            android:layout_height="450dp"
            android:layout_gravity="center_horizontal" />

        <Button
            android:id="@+id/signUpBtn"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/next_button_selector"
            android:text="Sign Up"
            android:textColor="#FFFFFF"
            android:textSize="20sp" >
        </Button>

        <Button
            android:id="@+id/registerWithFb"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:background="@drawable/next_button_xhdpi_facebook"
            android:text="Sign up with Facebook"
            android:textColor="#FFFFFF"
            android:textSize="20sp" >
        </Button>

    </LinearLayout>
</ScrollView>

现在,如果您希望空白区域具有精确的高度,这样 Button 就不会在 ScrollView 中下降太多,(我认为)您仅通过 XML 无法做到这一点,但您可以在 onCreate 方法中计算所需的高度:

final ScrollView mScrollView = (ScrollView)findViewById(R.id.my_scrollview);
final View whiteSpace = findViewById(R.id.whiteSpace);
final EditText password = (EditText)findViewById(R.id.passwordSignUp);
whiteSpace.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        // Check OS version as removeOnGlobalLayoutListener is available API>=11
        // and removeGlobalOnLayoutListener is deprecated in API<11
        if(Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
            whiteSpace.getViewTreeObserver().removeGlobalOnLayoutListener(this);
        } else {
            whiteSpace.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        }
        // Compute the height
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) whiteSpace.getLayoutParams();
        params.height = mScrollView.getHeight() - whiteSpace.getTop();
        whiteSpace.setLayoutParams(params);
    }
});

这是预期的输出: enter image description here

如果您想要不同的高度,只需更改分配的值

params.height = mScrollView.getHeight()-whiteSpace.getTop();

因为我的值被计算为所需的精确高度,所以按钮可以位于可见屏幕的正下方。计算另一个值取决于您的要求,这超出了您的问题范围。

关于java - 布局底部有两个按钮的 ScrollView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27518889/

相关文章:

swift - 尝试创建带有标签的 ScrollView

android - 如何将日期选择器放入 ScrollView 中

java - 处理 Servlet 中的异常

java - ClassNotFoundException : fuseki-server. jar

java - 区分同名的文件和文件夹 aws s3

android - 如何在wordpress中显示Android的xml文件?

java - Android 上的 HighChart 动态更新

java - 同步两个 Horizo​​ntalScrollViews Android

ios - 如何创建滚动静态图像的简单 ScrollView ?

java - android bytebuffer as intbuffer - asarray 总是给出不支持的操作异常