android - ScrollView 滚动,但不完全显示其内容

标签 android android-layout android-linearlayout textview android-scrollview

我使用这段代码将我的布局划分为 2 个 ScrollView

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

<RelativeLayout

    android:layout_width="match_parent"
    android:layout_height="0px"
    android:background="@drawable/ashokb"
    android:layout_weight="4" >
<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true" 
    android:background="@drawable/flagc">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" 
        android:layout_gravity="center">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="#000000" />

        <Button
            android:id="@+id/button1"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_marginLeft="-50dp"
            android:background="@drawable/ic_menu_share" />

    </LinearLayout>

    <TextView
            android:id="@+id/textView5"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Medium Text"
            android:textColor="#000000" 

            android:gravity="center"
            android:textAppearance="?android:attr/textAppearanceMedium" />

  </LinearLayout>
</ScrollView>
</RelativeLayout>

<RelativeLayout

    android:layout_width="match_parent"
    android:layout_height="0px"
    android:background="@drawable/ashokb"
    android:layout_weight="1" >

    <ScrollView
        android:id="@+id/scrollView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" >

        <TextView
            android:id="@+id/textView3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"       
            android:text="Medium Text"
               android:textColor="#000000"      
            android:textAppearance="?android:attr/textAppearanceMedium"
             />

    </ScrollView>

</RelativeLayout>
</LinearLayout>

这在 GraphicalLayout 中显示如下 enter image description here

在设备和模拟器中, View 在对话框中如我所愿变得正确:enter image description here

问题: 如果 textview 中的文本很小,那么第一个 scrollview 会像预期的那样表现正常,但是如果 texview 中的文本很大,那么即使完全滚动后,完整的文本也不可见,相反它在滚动末尾显示空白。如视频所示

http://youtu.be/fIqzTQ8neGs

在最后点击的项目中看到,第一个 ScrollView 没有完全显示其内容。

我希望你把这个问题弄清楚。

有什么解决办法吗?

最佳答案

基本上,您在布局文件中设置了许多不必要的属性。但是你的问题是由 android:layout_gravity 属性设置在你的顶部 ScrollView 中的第一个 LinearLayout 引起的:

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" 
        android:layout_gravity="center">

删除 android:layout_gravity="center" 将解决此 ScrollView 中内容被裁剪的问题。


顺便说一句:如果其中的内容小于 ScrollView 并且您想将其居中放置,您应该使用不同的方法。您不想让子项直接在 ScrollView 中居中,但您应该用它的唯一子项填充 ScrollView 并将内容居中在这个子项中。通常你会在这个 LinearLayoutandroid:gravity="center" 上设置 android:layout_height="match_parent"。但在 ScrollView 中,您不能使用这种方法 - ScrollView 直接子项的高度应始终为 wrap_content 并将其设置为 match_parent 不会工作。然而在 ScrollView 中有一个属性允许这样的行为,它被称为 setFillViewport(boolean fillViewport)您应该将其设置为 true,如下所示:

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:fillViewport="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" 
        android:gravity="center">

除了 ScrollView 中的以下几行之外,没有任何意义。它基本上将此 ScrollView 的引力设置为 center,但由于它与其父级的大小相匹配 - 它不能位于中心。

    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"

顺便说一句。第二个 ScrollView 不应将 android:layout_height 设置为 wrap_content。请将其更改为 match_parent

此外,您真的应该尝试简化布局和属性数量(尤其是所有“重力”:)

关于android - ScrollView 滚动,但不完全显示其内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25036546/

相关文章:

android - 我应该如何将元素添加到自定义相机 android 应用程序?

java - 将 Jersey 客户端添加到我的 Android 应用程序。发生非法参数异常?

android - TextInputLayout 的 float 提示文字大小

android - Eclipse 更新后无法启动

android - SwipeRefreshLayout - 向下滑动刷新但不移动 View 下拉

android 将 listview 项目高度设置为所有 child 中最高的,所以看起来像带边框的表格

android - 在线性布局中以编程方式移动布局位置

android - 以编程方式设置 LinearLayout 的重力

android - 不同配置文件的查询 Intent Activity : Android for Work

android text 在垂直方向查看文本