简体   繁体   中英

How to make TextView fill only the available space

Ive looked at the similar answers on SO but they didnt work for me. I have the following layout which may not be optimum (please point out any mistakes as I am a total beginner).

<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent" android:rowCount="3" android:columnCount="1" android:padding="0dp"
              android:background="#ebe0cf">

    <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:layout_row="0" android:layout_column="0"
            android:background="#ebe0cf" android:layout_marginTop="5dp">
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Book Name"
                android:id="@+id/textView" android:layout_gravity="center_horizontal" android:textSize="30dp"
                android:textStyle="bold"/>
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Chapter Name"
                android:id="@+id/textView1" android:textSize="13dp" android:layout_gravity="center_horizontal"
                android:layout_marginTop="-3dp"/>
    </LinearLayout>


    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."
              android:id="@+id/textView4" android:padding="5dp" android:background="#ffffff"
              android:layout_margin="10dp"
            android:scrollbars="vertical"/>

    <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:layout_row="2" android:layout_column="0"
            android:background="#ebe0cf">
        <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Prev"
                android:id="@+id/button"/>
        <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Next"
                android:id="@+id/button1"/>
        <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Language"
                android:id="@+id/button2"/>
    </LinearLayout>
</GridLayout>

What is happening is once the TextView has long enough text it pushes the buttons off the page, I would like the buttons to stay in place at the bottom and for the TextView to start scrolling... I tried placing it inside a ScollView and the same happened.

I would like to keep it simple, so if possible I would like to avoid using things like relative layouts.

If you give you LinearLayout a weightSum (any arbitrary number) and then give its child Views layout_weight that adds up to that sum, it will use those weights as percentages of the total.

So your buttons can be layout_weight="1" and your ScrollView can be layout_weight="9" . If the LinearLayout has a weightSum="10" then it will be 90% ScrollView and 10% buttons.

The weights don't have to be integers, you can have a weight of 5.5.

Oh, and this is very very important, set the layout_height of the child Views (assuming your LinearLayout has an orientation of Vertical) to 0dp. The weight will take care of how tall it *really is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM