顶部视图在 ScrollView 中消失

Top view disappeared in ScrollView

我正在向 LinearLayout 动态添加 table 布局列表。为此,我使用 ScrollView 查看添加到线性布局的 table 列表。

下面是我的 xml 代码。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:background="@color/white"
tools:context=".MainActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="RET MASTER SITE REPORTS"
    android:paddingBottom="5dp"
    android:textColor="@android:color/black"
    android:textStyle="bold" />

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:orientation="vertical"></LinearLayout>

</ScrollView>

源代码:

layout = (LinearLayout) findViewById(R.id.linearLayout);
for (int i = 0; i < 5; i++) {
        if (isColorChange) {
            displayDeviceDetails(getColor(R.color.blue), getColor(R.color.lightBlue));
        } else {
            displayDeviceDetails(getColor(R.color.orange), getColor(R.color.lightOrange));
        }
}



 //Method which i am calling from loop
 private void displayDeviceDetails(int titleColor, int cellColor) {
    TableRow row = null;
    TableLayout tableLayout = new TableLayout(this);
    tableLayout.setPadding(0, 30, 0, 0);
    for (int i = 0; i < arr.length; i++) {
        row = (TableRow) inflater.inflate(R.layout.child_views, null);
        row.setBackgroundColor(titleColor);
        TextView tvTitle = (TextView) row.findViewById(R.id.tvRowTitle);
        TextView tvText = (TextView) row.findViewById(R.id.tvRowText);
        if (i == 0) {
            tvText.setVisibility(View.GONE);
            tvTitle.setGravity(Gravity.CENTER);
            tvTitle.setTextColor(Color.WHITE);
            tvTitle.setText(arr[i]);
        } else {
            changeBgColor(cellColor);
            setBgColor(tvTitle);
            setBgColor(tvText);
            tvTitle.setGravity(Gravity.RIGHT);
            tvTitle.setText(arr[i]);
            tvText.setText(arr1[i]);
            row.setBackgroundColor(titleColor);
        }
        tableLayout.addView(row);
    }
    layout.addView(tableLayout);

}

在上面的代码中,屏幕顶部有一个TextView,用于显示一些文本。我在线性布局中添加了 3 tables,它从中间显示第一个 table,然后是下一个 2 tables,底部有一些白色 space/empty 屏幕。如果我增加 table 的数量,底部屏幕空白 space 就会增加。

示例:ScrollView 中有 5 个 table,但它会从第 2 个 table 开始显示,这可能是从 table 开始,也可能是从 [= 的中间开始38=],然后是第 3、4、5 个 tables,ScrollView 底部为空 space。

我可以看到文本,但我在 ScrollView 内的线性布局中添加了 5 tables,当我执行它时,它将从第 2 个 table 开始显示,这可能来自 table 开始或可能从中间开始,然后是第 3、4 和 5 tables,其中 space 为空。我附上了我的屏幕截图。

请帮帮我。

您需要将根 LinearLayout 更改为 RelativeLayout,如下所示:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:background="@color/white"
tools:context=".MainActivity">

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="RET MASTER SITE REPORTS"
    android:paddingBottom="5dp"
    android:textColor="@android:color/black"
    android:textStyle="bold" />

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/textView">

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:orientation="vertical"></LinearLayout>

</ScrollView>
</RelativeLayout>

希望对你有帮助

找到了!罪魁祸首是 LinearLayout 中的 android:layout_gravity="center_vertical"..
把这个删掉就好了。