【问题标题】:Android scrollview hiding top content in layoutAndroid滚动视图隐藏布局中的顶部内容
【发布时间】:2012-04-12 13:33:45
【问题描述】:

我遇到了一个问题,android 滚动视图开始隐藏我在布局顶部的一对文本视图,我在这个网站上找到了另一个人,他遇到了这个问题,不幸的是,帮助的人能够得到帮助他们实际上并没有说什么修复它,谁能告诉我这里的修复是什么?

android scrollview hiding top content

任何帮助都会很大,这是我的 xml 代码:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/title_color_dark_transparent" >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_vertical|center_horizontal|center"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="5dp"
        android:orientation="vertical"
        android:paddingTop="5dp" >

        <TextView
            android:id="@+id/saysomething"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/loading"
            android:textColor="#FFFFFF"
            android:textSize="30dp" />

        <TextView
            android:id="@+id/saysomethinginfo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:text="@string/loading"
            android:textColor="#FFFFFF"
            android:textSize="17dp" />

        <EditText
            android:id="@+id/tweetedittext"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/edit_text"
            android:gravity="top"
            android:hint="@string/edittext_hint"
            android:inputType="textCapSentences|textMultiLine|textAutoCorrect|textAutoComplete"
            android:lines="3"
            android:textAppearance="@style/TextAppearance.EditText"
            android:textSize="17dp" />

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginTop="5dp"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/photobutton"
                style="@style/TextAppearance.Button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/button_background"
                android:onClick="photobuttonClicked"
                android:text="@string/photobuttontext"
                android:textSize="15dp" />

            <TextView
                android:id="@+id/charactersremaining"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_centerInParent="true"
                android:layout_toLeftOf="@+id/posttweetbutton"
                android:text="@string/characters"
                android:textColor="#FFFFFF"
                android:textSize="20dp" />

            <Button
                android:id="@id/posttweetbutton"
                style="@style/TextAppearance.Button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_marginLeft="2dp"
                android:background="@drawable/button_background"
                android:onClick="posttweetbuttonClicked"
                android:text="@string/postbuttonstext"
                android:textSize="15dp" />
        </RelativeLayout>
    </LinearLayout>

    <TextView
        android:id="@+id/photodetails"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:textColor="#FFFFFF"
        android:textSize="17dp"
        android:layout_marginTop="8dp"
        android:layout_marginLeft="8dp" />

    <ImageView
        android:id="@+id/iv_pic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp" />
</LinearLayout>

</ScrollView>

【问题讨论】:

  • 我看不到您的 ScrollView 上方的任何 TextView。您还缺少 ScrollView 上的结束标记。
  • 似乎我错误地没有在滚动视图的结束标记中发表评论,但它现在在那里,文本视图是您在线性布局内看到的第一个,将文本视图放在线性布局之外给我一个错误,因为显然您在滚动视图中不能有多个孩子
  • 我以为你不想让你的 ScrollView 隐藏那些 TextView?也许你应该解释一下你真正想要的布局是什么样的,因为我不是很清楚。
  • 布局对于屏幕来说太大了,所以我把它放在一个滚动视图中,但是当我在图像视图中设置图像时,2个文本视图“@+id/saysomething”和“@+id/ saysomethinginfo”是隐藏的,不能滚动查看它说什么,我需要的是让用户能够向上滚动并能够看到隐藏在下面的内容
  • 这可能会有所帮助stackoverflow.com/questions/8280051/…

标签: android android-layout android-xml android-scrollview


【解决方案1】:

找到了! 罪魁祸首是LinearLayout 中的android:layout_gravity="center"。只需删除它,一切都会好起来的。

【讨论】:

  • 谢谢你 - 在这个问题的实例中为我工作。
  • 有效,如果您仍然需要将视图居中,请使用以下解决方法:pivotallabs.com/centering-a-view-within-a-scrollview
  • 非常感谢@Sylphe,奇怪的是你的回答未被接受:(
  • 非常感谢。我遇到了类似的问题,但在HorizontalScrollView 中。这不应该作为谷歌代码的错误提交吗?至少,应该添加 lint 来警告这一点。
  • 删除 'android:layout_gravity="center"' 对我有帮助!哥们,谢啦! :)))))))))))))
【解决方案2】:

你试过设置吗

android:fillViewport="true"

在滚动视图中?

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:fillViewport="true"
  android:background="@color/title_color_dark_transparent" >

【讨论】:

    【解决方案3】:

    您可以通过编程方式滚动。

    1. 从 xml 中移除 android:layout_gravity 属性
    2. 在 onResume 函数中添加滚动

    我项目中的一些代码:

    protected void onResume() {
        super.onResume();
        final HorizontalScrollView svInMenu = (HorizontalScrollView) findViewById(R.id.svInMenu);
        ViewTreeObserver vto = svInMenu.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
            public void onGlobalLayout() {
                svInMenu.scrollTo(svInMenu.getRight() / 4, 0);
            }
        });
    }
    

    【讨论】:

      【解决方案4】:

      您的外部LinearLayout 的高度设置为match_parent。它应该是wrap_content。内部LinearLayout 也应该有高度wrap_content。

      【讨论】:

        【解决方案5】:

        将 android:layout_weight="1" 添加到您的滚动视图中。它将解决问题。像这样的

         <ScrollView
                android:id="@+id/scroll"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:layout_weight="1" >
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-07-25
          • 1970-01-01
          • 1970-01-01
          • 2020-02-13
          • 1970-01-01
          • 1970-01-01
          • 2021-04-27
          • 1970-01-01
          相关资源
          最近更新 更多