android - ScrollView 不显示顶部

标签 android android-layout

问题:当 ScrollView 超出一定高度时,它会隐藏其 subview 的顶部。

我在 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 现在可以正常工作了。感谢大家的建议。

关于android - ScrollView 不显示顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10798401/

相关文章:

android - ListView 中的动态列数

android - 以编程方式膨胀的 RadioButton layout_width 与布局文件不匹配

android - Fragment 之间通过 Intent 进行的数据交换适用于 5.0 之前的版本,但不适用于 Android 5.0

android - 从 EditText 保存文本

android - 如何在 Android 中设置搜索栏值?

android - 从 AppWidget 打开 PopUpWindow

android - 嵌入在 Android 应用程序中的网页

Android 布局限定符

安卓 : Create a button with border bottom

java - Android:单击按钮时切换相机