2

Имеется 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

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

1
  • Ещё можно поиграть с параметром android:lines у TextView, чтобы текст занимал определённое количество строк.
    – user32495
    10 мар 2015 в 12:47

3 ответа 3

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

3
  • Не знал о таком приеме, спасибо. Но только эффект тот же самый. При большом количестве текста кнопок - нет.
    – IronVbif
    15 фев 2011 в 16:55
  • @IronVbif Должно работать. Не забудьте убрать ScrollView.
    – stanislav
    15 фев 2011 в 17:20
  • Так и не понял что не так делаю. Получилось только с помощью Relative Layout расположить кнопки снизу экрана, но тогда они закрывают текст. А с Skeleton App у меня кнопки друг под другом оказываются.
    – IronVbif
    16 фев 2011 в 19:10
1

Всё логично - TextView при большом размере вытесняет кнопки с экрана. Можно всё сделать в RelativeLayout

<?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">

    <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>           
    <TextView 
       android:text="TextView and many many many character" 
       android:id="@+id/textView_Question" 
       android:scrollbars="vertical"
       android:layout_alignParentTop="true"
       android:layout_above="@id/button_yes"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:padding="10dip"
       android:textSize="30px">
   </TextView>
</RelativeLayout>

Либо можно оставить LinearLayout и задать TextView атрибут android:layout_weight="1". Первый вариант лучше, т.к. требует создания меньшего количества View.

Примерно на эту тему есть ещё статья от Romain Guy.

0

Попробуй так:

<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="0dp"
    android:layout_weight="1"
    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"/>

</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 
        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"/>

</RelativeLayout>

</LinearLayout>

Обрати внимание на:

android:layout_height="0dp" и 
android:layout_weight="1" в ScrollView

Ваш ответ

Нажимая на кнопку «Отправить ответ», вы соглашаетесь с нашими условиями использования и подтверждаете, что прочитали и поняли наши политику конфиденциальности и нормы поведения.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.