【问题标题】:Android: how to show a TextView below a ScrollView layout?Android:如何在 ScrollView 布局下方显示 TextView?
【发布时间】:2015-12-30 01:27:44
【问题描述】:

我试图在 UI 底部显示一个带有居中 TextView 的矩形框作为页脚栏(就像工具栏/操作栏显示为主 UI 上方的页眉栏一样)。 TextView 应该位于主 UI(这是一个 ScrollView)下方,并且 TextView 根本不显示。其他一切都正确显示。关于我做错了什么有什么想法吗?

layout.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:focusableInTouchMode="true"
tools:context=".CardViewActivity">

<include
    android:id="@+id/toolbar"
    layout="@layout/toolbar" >
</include>

<ScrollView
    android:id="@+id/ScrollView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    ...>
...
</ScrollView>

<TextView
    android:id="@+id/skychilltext2"
    android:text="skychill"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:textStyle="bold"
    android:textColor="#FFFFFF"
    android:background="@color/colorPrimary"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:clickable="true"  />

</LinearLayout>    

【问题讨论】:

    标签: android android-layout textview


    【解决方案1】:

    因为您将高度设置为match_parent,所以它将占据所有房间。我不确定你到底想要什么,但你可以给它增加一个重量以扩大到尽可能多的空间。

    <ScrollView
        android:id="@+id/ScrollView1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" 
        ...>
        ...
    </ScrollView>
    

    权重指定视图将占据屏幕的多少百分比,因此如果每个视图具有相同的权重,它们将在屏幕上占据相同的空间。

    view 1 - weight 1
    view 2 - weight 1
    view 3 - weight 1
    view 4 - weight 3 since this is weight 3 and all the weights above add up to
                    | 3, this view will take up exactly half the screen.
                    | the other ones will take 1/6 of the screen because (1+1+1+3 = 6)
    

    【讨论】:

    • 好的,那么我需要为 TextView 设置任何 layout_weight 还是直接省略?
    • 别管它。没必要。保留它来包装内容
    • 所以这意味着 TextView 的 View 只会足够大来包裹文本内容,并且所有剩余的垂直空间都将分配给 ScrollView,对吗?
    【解决方案2】:

    试试下面

    <RelativeLayout
      (...)>
    
        <LinearLayout android:id="@+id/ll1"
            android:layout_alignParentTop="true"
            (...)/>
    
        <TextView android:id="@+id/button"
            android:layout_alignParentBottom="true"
            (...)/>
    
        <ScrollView
             android:layout_above="@id/button"
             android:layout_below="@id/ll1"
             (...)/>
    
    </RelativeLayout>
    

    参考这里How to make a static button under a ScrollView?How can I place new items under scrollview

    【讨论】:

    • 好的,从 LinearLayout 切换到 RelativeLayout 有什么缺点吗?
    【解决方案3】:

    问题是滚动视图中的android:layout_height="match_parent"。它指示 LinearLayout 给它所有的高度,而没有给 TextView 留下任何东西。

    更改滚动视图:

    android:layout_height="wrap_content"
    android:layout_weight="1"
    

    更改文本视图:

    android:layout_weight="0"
    

    【讨论】:

    • 好的,将 TextView 的 layout_weight 设置为零有什么作用?与根本不设置权重相比?
    • 具有 layout_weight="0" 的视图不会扩展以占用 LinearLayout 中的可用空间
    • 我不明白。将 TextView 的 layout_weight 设置为零则意味着 TextView 根本不会显示,因为线性布局中没有可用空间?
    • 没有。值“0”表示它不会参与额外可用空间的分配,它只会占用其内容所需的空间。
    【解决方案4】:

    在 TextView 上设置 android:layout:weight。

    【讨论】:

      【解决方案5】:

      您需要 weightsum 来缩放布局。您的代码为高度滚动视图设置了 match_parent

      <ScrollView
      android:id="@+id/ScrollView1"
      android:layout_width="match_parent"
      android:layout_height="wrap_content" 
      ...>
      

      ...

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-12-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-04-11
        • 1970-01-01
        • 2021-11-03
        • 1970-01-01
        相关资源
        最近更新 更多