Проблема комбинирования ScrollView и RelativeLayout

Рейтинг: 2Ответов: 1Опубликовано: 15.02.2011

Имеется xml разметка для экрана.

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

    <ScrollView
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:id="@+id/scrollView1">
        <TextView 
            android:text="TextView and many many many many many many many character" 
            android:id="@+id/textView_Question" 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:padding="10dip"
            android:textSize="30px">
        </TextView>
    </ScrollView>
    <RelativeLayout 
        android:layout_width="fill_parent" 
        android:id="@+id/Layout2" 
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <Button 
            android:text="ButtonYes" 
            android:id="@+id/button_yes" 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentBottom="true">
        </Button>
        <Button 
            android:text="ButtonNo" 
            android:id="@+id/button_no" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentBottom="true">
        </Button>           
    </RelativeLayout>
</LinearLayout>

alt text

Но если TextView будет содержать много текста, то кнопки пропадают. Текст при этом прокручивается.

alt text

Как сделать, чтобы кнопки не исчезали?

Ответы

▲ 5Принят

Чтобы прокручивать TextView не обязательно помещать его внутрь ScrollView, TextView умеет сам прокручиваться. Нужно изменить разметку TextView, добавив атрибут

android:scrollbars = "vertical"

и изменить код активности, задав метод прокрутки

import android.text.method.ScrollingMovementMethod;

...

mTexView.setMovementMethod(new ScrollingMovementMethod());

а ScrollView - убрать.

Пример на основе Skeleton App из Android SDK. Вместо EditText добавить TextView:

<TextView android:id="@+id/editor"
    android:layout_width="match_parent"
    android:layout_height="0dip"
    android:autoText="true"
    android:capitalize="sentences"
    android:layout_weight="2"
    android:freezesText="true"
    android:textSize="120dip"
    android:scrollbars = "vertical" >
</TextView>

В итоге получаем до прокрутки:

alt text

и после прокрутки:

alt text