如何使TextView仅填充可用空间

时间:2013-10-31 22:45:46

标签: android layout scroll scrollview grid-layout

我看了SO上的类似答案,但它们对我没有用。我有以下布局可能不是最佳的(请指出任何错误,因为我是一个初学者)。

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

发生的事情是,一旦TextView有足够长的文本它将按钮推离页面,我希望按钮保持在底部的位置,并让TextView开始滚动...我试着把它放在一个ScollView也是如此。

我想保持简单,所以如果可能的话我想避免使用相对布局之类的东西。

1 个答案:

答案 0 :(得分:0)

如果你给LinearLayout一个weightSum(任意数字),然后给它的子视图layout_weight加起来总和,它将使用这些权重作为总数的百分比。

因此,您的按钮可以是layout_weight="1",您的ScrollView可以是layout_weight="9"。如果LinearLayout具有weightSum="10",那么它将是90%ScrollView和10%按钮。

权重不一定是整数,你的权重可以是5.5。

哦,这非常非常重要,将子视图的layout_height(假设您的LinearLayout的方向为Vertical)设置为0dp。重量将照顾它真正的高度。