【问题标题】:Top view disappeared in ScrollView顶部视图在 ScrollView 中消失了
【发布时间】:2015-08-06 04:49:32
【问题描述】:

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

下面是我的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 个表格,它显示了中间的第一个表格,然后是接下来的 2 个表格,底部有一些空白/空白屏幕。如果我增加底部屏幕空白空间正在增加的表格数量。

示例:ScrollView 中有 5 个表格,但它会从可能从表格开始或可能从表格中间开始的第二个表格显示,然后是第三、第四和第五个表格,底部有空白空间滚动视图。

我可以看到文本,但是我在 ScrollView 内的线性布局中添加了 5 个表,当我执行该操作时,它将从可能从表开始或可能从中间开始的第二个表显示,然后是第三个、第四个n 第 5 桌有空位。我有附件我的屏幕截图。

请帮帮我。

【问题讨论】:

  • 插入表格时可能有问题
  • 我已经添加了我的源代码。一旦检查。问题仅在于滚动视图。

标签: android scrollview


【解决方案1】:

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

【讨论】:

  • 但我真的很震惊,滚动视图会有什么效果??
  • 是的,当我知道这一点时我也很震惊,但这是事实!
【解决方案2】:

您需要将根 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>

希望对你有帮助

【讨论】:

  • 无效。这不是根布局的问题。
  • 我试过了,但没有效果。无论如何,拉维的答案是正确的。感谢您的回复。
猜你喜欢
  • 2013-04-17
  • 2016-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多