android - RelativeLayout 设计问题 : ScrollView between a RelativeLayout and a Button

标签 android scrollview android-relativelayout

我的 Activity 布局必须如下所示:

  • 屏幕分为 3 个部分(3“行”)
  • 第一部分由左侧的图像和旁边的两个 TextView 组成。
  • 第二部分是一个大文本,放入 ScrollView 中以便滚动。
  • 第三部分只是一个按钮,这个按钮必须始终位于屏幕底部。

因此,第 1 和第 3 部分都必须固定,只有第 2 部分可以滚动。

这是我的代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout0"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

<RelativeLayout
    android:id="@+id/relativeLayout1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"            
        android:layout_toRightOf="@id/imageView1"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"            
        android:layout_toRightOf="@id/imageView1"
        android:layout_below="@id/textView1"
        android:text="TextView" />

</RelativeLayout>

<RelativeLayout 
    android:id="@+id/relativeLayout2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/relativeLayout1">      

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >    

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text " />

    </ScrollView>

</RelativeLayout>

<RelativeLayout 
    android:id="@+id/relativeLayout3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@id/relativeLayout2">

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</RelativeLayout>

</RelativeLayout>

这里的问题很明显:

enter image description here

中间部分的文本可以正常滚动,但问题是当按钮在布局中“开始”时,ScrollView 不会“结束”。

最佳答案

试试这个:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:src="@drawable/ic_launcher" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/imageView1"
            android:text="TextView" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/textView1"
            android:layout_toRightOf="@id/imageView1"
            android:text="TextView" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/relativeLayout2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_below="@id/relativeLayout1" >

        <ScrollView
            android:id="@+id/scrollView1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/textView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text BIG BIG text " />
        </ScrollView>
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/relativeLayout3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/relativeLayout2" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
    </RelativeLayout>

</LinearLayout>

关于android - RelativeLayout 设计问题 : ScrollView between a RelativeLayout and a Button,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8050321/

相关文章:

android - ListView 项目点击问题

android - 如何以编程方式将背景颜色设置为表格布局中的按钮?

java - 如果我将空值传递给方法,应用程序会因空指针异常而崩溃

ios - 用于设置多步骤注册页面的最佳 ViewController 是什么?

java - 在 relativeLayout 中将 View 设置在中心下方几处

android - 三星 Galaxy J1 4G 上的 webRTC 应用程序崩溃

android - 在 ScrollView 中使用 onOverScrolled

ios - Xcode 9 中的 UIKeyboard Notification.userInfo 关键错误

android - 如何在android中以编程方式更改relativelayout中 View 的x

java - 设置边距时,相对布局中的按钮会被压扁