ScrollView不显示顶部

2024-03-04

问题:当 ScrollView 增长超过一定高度时,它会隐藏其子视图的顶部。

我在 XML 中定义了以下布局

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

    <LinearLayout
        android:id="@+id/commandPanel"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:paddingLeft="5dip"
        android:paddingRight="5dip" >

        <Button
            android:id="@+id/button1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="OK" />

        <Button
            android:id="@+id/button2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Back" />
    </LinearLayout>

    <RelativeLayout
        android:id="@+id/mainContentPanel"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@id/commandPanel"
        android:background="@android:color/white" >

        <ScrollView
            android:id="@+id/formScrollView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_gravity="center"
            android:layout_marginTop="5dip"
            android:layout_weight="1"
            android:background="@android:color/darker_gray"
            android:fillViewport="false"
            android:paddingBottom="5dip"
            android:scrollbarStyle="insideOverlay" >

            <LinearLayout
                android:id="@+id/formContentLayout"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_gravity="center"
                android:layout_marginTop="20dip"
                android:background="@android:color/black"
                android:gravity="center_horizontal|top"
                android:orientation="vertical"
                android:paddingLeft="5dip"
                android:paddingRight="5dip"
                android:paddingTop="20dip" >

                <LinearLayout
                    android:id="@+id/tr"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dip"
                    android:orientation="horizontal" >

                    <TextView
                        android:id="@+id/title"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="sample title" />

                    <TextView
                        android:id="@+id/value"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="sample value ......." />
                </LinearLayout>
            </LinearLayout>
        </ScrollView>
    </RelativeLayout>

</RelativeLayout>

- - - - - 代码 - - -

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.scrollview);
    updateUi();
}

//add few more rows here
void updateUi() {
    LinearLayout sampletr = (LinearLayout) findViewById(R.id.tr);
    LinearLayout contentPane = (LinearLayout) findViewById(R.id.formContentLayout);
    TextView title = (TextView) findViewById(R.id.title);
    TextView value = (TextView) findViewById(R.id.value);
    for (int i = 0; i < 25; i++) {
        LinearLayout tr = new LinearLayout(this);
        TextView t = new TextView(this);
        t.setText("Sample title : " + i);
        TextView v = new TextView(this);
        v.setText("Sample value : " + i);

        tr.addView(t, title.getLayoutParams());
        tr.addView(v, value.getLayoutParams());
        contentPane.addView(tr, sampletr.getLayoutParams());
    }
}

如果没有,LinearLayout(ScrollView 的直接子级)中的顶部行开始从顶部消失。行数增长超过 15 行!此外,我什至无法手动向上滚动来查看它们,请问有什么指针,这个布局有什么问题吗?


好的,我已经通过从 LinearLayout (ScrollView 的直接子级)中删除以下行解决了这个问题

android:layout_gravity="center"

ScrollView 现在可以正常工作了。谢谢大家的建议。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ScrollView不显示顶部 的相关文章

随机推荐

  • Android 4.2.2+ 上的叠加状态栏

    我想覆盖 android 状态栏 就我而言 它位于顶部 我不想覆盖或隐藏导航栏 Note 解决方案必须在 Android 4 2 2 上运行 我更喜欢非 root 设备的答案 我搜索了很多 SO 问题和答案 但没有一个适用于 4 2 2 下
  • 用一个变量求大量函数的根

    我正在使用 Python numpy scipy 编写一个小型光线追踪器 表面被建模为二维函数 给出法线平面上方的高度 我将寻找射线和表面之间的交点的问题简化为寻找具有一个变量的函数的根 这些函数是连续且连续可微的 有没有一种方法可以比使用
  • 使用模板删除任何容器

    我有很多代码要删除其中包含指针的向量容器 也就是说 我必须先删除所有指针 然后清除容器 每次我都会手动迭代并删除指针 我知道 stl algorithms for each 为了规避所有这些代码 我创建了一个模板来删除所有具有指针的向量容器
  • 跳转到下一个空白处

    Instead of using w to jump to the beginning of the next word or using e to jump to the end of the next word I want to ha
  • 来自不同线程的触发事件

    在我的主窗口中 Thread A 我开始一个新线程 Thread B 它在用户等待时做了一些工作 如果出现错误或需要用户提供额外信息 线程 B 会触发事件 线程 A 将侦听这些事件 在线程 A 的事件侦听器中 我需要向用户显示对话框消息 我
  • git 如何处理带空格的文件夹名称

    我有一个名为31 Files of oop 当我尝试运行此命令时 git rm rf 31 文件 oop 我收到这个致命错误 致命 pathspec 31 与任何文件都不匹配 我需要如何准备带空格的文件夹名称以便在 git 中使用 尝试这样
  • 如何将动态选择菜单和嵌套属性一起包含在 Rails 中?

    考虑我有三个表用户 国家 地区 州 我有一个添加新用户的页面 当我添加新用户时 我必须在选择框中列出国家 地区 并且在选择国家 地区时 多个选择框应加载该国家 地区的州 我应该能够选择所需的状态 同样 我可以单击添加按钮添加另一个选择框并选
  • Laravel 电子邮件验证 - 强制登录

    安装启用新的内置电子邮件验证后 一切正常 注册后发送电子邮件并单击激活启用帐户 但是 我面临着用户必须登录才能进行验证过程的情况 这意味着 如果用户在使用验证链接之前未登录 他将被重定向到登录页面 然后显示 resources view a
  • 为什么 observable.subscribe 只能在构造函数中工作

    在开发 Ionic 3 应用程序时 我遇到了当你订阅时的问题observable从服务于ngOnInit并将局部变量更新到其中 它不会更新视图 例如 HTML模板 p myVariable p constructor myService M
  • 如何正确实现自定义ajax

    为了保留标志文字 div class small 7 medium 4 columns logo 和菜单 div
  • 无法导入名称包括[关闭]

    Closed 这个问题需要调试细节 help minimal reproducible example 目前不接受答案 我按照 django 中的教程进行操作并收到以下错误 找到了一些解决方案 但它没有解决我的错误 教程链接是this ht
  • 有什么方法可以从 jinja2 模板字符串中获取变量键吗?

    这是我初始化 jinja2 模板的代码片段 from jinja2 import Template templ Template foo to bar 我愿意从模板 obj 中提取模板字符串变量键 如下所示 templ keys foo b
  • 连续字母频率

    我正在尝试编写代码来确定字符串中字母的连续频率 例如 aabbcbb gt a 2 b 2 c 1 b 2 我的代码给出了第一个字母的频率 但不会继续到下一个字母 def encrypt str array count 0 str each
  • Spring集成网关“Dispatcher没有订阅者”

    我遇到了异常Dispatcher has no subscribers on the outboundChannel并且不明白为什么 我确信它很简单 我已将代码精简为下面一个非常简单的示例 我的背景是
  • 在子文件夹中使用 Node.js 应用程序的“工作目录”选项以及压缩/解压缩工件步骤时,GitHub Actions 部署错误

    我有一个简单的 Node js 服务器 我正在使用 GitHub Actions 中的以下工作流程将其部署到 Azure 应用服务 它包括工件压缩和解压缩步骤这个答案 https stackoverflow com a 69402502 1
  • 如何在 Flex 中克隆对象?

    我想克隆一个 Canvas 对象 其中包含具有多个几何形状的 Degrafa Surface 我尝试了天真的方法 return ObjectUtil copy graph area as Canvas 这导致了错误 TypeError Er
  • 有没有办法改变 flutter 应用程序中播放的音频的音调?

    再会 有没有办法让我使用插件来改变通过 flutter 应用程序播放的声音的音调 我看还是没有答案 您可以使用 just audio 包中的 setPitch 方法 如下所示 await audioPlayer setPitch 1 5 链
  • kubectl - 错误:您必须登录到服务器

    我已经使用 kubeadm 安装了 k8s 配置了 OIDC 配置完成后 当我运行命令时kubectl email protected cdn cgi l email protection get nodes I get 错误 您必须登录到
  • 识别两个表之间差异的最快方法?

    我需要根据事务存档表检查活动表 但我不确定执行此操作的最快方法 例如 假设我的实时表由以下列组成 Term CRN Fee 级别代码 我的存档表将具有相同的列 但也具有存档日期 以便我可以查看活动表在给定日期的值 现在 我将如何编写查询来确
  • ScrollView不显示顶部

    问题 当 ScrollView 增长超过一定高度时 它会隐藏其子视图的顶部 我在 XML 中定义了以下布局