android - scrollview 导致出现黑色背景

标签 android scrollview

我只是像这样添加一个 ScrollView ,在屏幕加载后出现黑色背景。

这个黑色区域位于周围 RelativeLayout 的左侧和最上角。 ScrollView 使用 android:layout_marginLeft="20dp"定位 android:layout_marginTop="40dp" 所以左边的 20dp 和上面的 40dp 是黑色的,而剩下的灰色背景是原状的。

这里是带有 scrollView 的 xml 部分:

  <View
            android:id="@+id/emptyView"
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:layout_below="@+id/right1" />

        <RelativeLayout
            android:id="@+id/right2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/emptyView" >

            <RelativeLayout
                android:id="@+id/right22"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/emptyView" >

                <ImageView
                    android:id="@+id/emptyView2"
                    android:layout_width="match_parent"
                    android:layout_height="2dp"
                    android:contentDescription="@string/anyStringValue" />

                <HorizontalScrollView
                    android:id="@+id/scrollView"
                    android:layout_width="fill_parent"
                    android:layout_height="520dp"
                    android:layout_marginLeft="20dp"
                    android:layout_marginTop="40dp"
                    android:background="#0000FF" >

                    <TextView
                        android:id="@+id/infoTxt"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:background="#FF0000"
                        android:padding="10dp"
                        android:text="@string/Settings_infoTxt"
                        android:textColor="#000000"
                        android:textSize="20dp" />
                </HorizontalScrollView>
            </RelativeLayout>
        </RelativeLayout>

我已经尝试在 ScrollView 以及 2 个 RelativeLayouts 之上添加一个空 View 。但是黑色区域无论如何都会出现。 (有/没有 RelativeLayouts 和顶部的空 View )

由于整个页面的背景是灰色的,这个黑色区域扭曲了整个屏幕。

我以前多次使用 ScrollView ,但从未遇到过这样的问题。我不知道是什么原因造成的。

如何去除 ScrollView 造成的黑色区域?

非常感谢!

最佳答案

我遇到了几乎相同的问题,加载 ScrollView 时出现了一些黑色区域,这些区域在触摸时消失了。 我通过不设置 ScrollView 的背景颜色来解决它。设置内容 View 的背景颜色应该就足够了。

      //outer layout, where black shapes appear
    LinearLayout outer = new LinearLayout(context);
    outer.setBackgroundColor(Color.MAGENTA);

    ScrollView list = new ScrollView(context);
    list.setLayoutParams(new LayoutParams(100, 300));

    //        do not set the background color here, this causes the blak shapes
    //        list.setBackgroundColor(Color.CYAN);
    //        add an inner layout to the scrollView and set the background of the innerlayout
    LinearLayout linearLayout = new LinearLayout(context);
    linearLayout.setBackgroundColor(Color.CYAN);
    linearLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));

    list.addView(linearLayout);
    outer.addView(list);

关于android - scrollview 导致出现黑色背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10199201/

相关文章:

android - 如何在使用 Intent.ACTION_PACKAGE_REMOVED 时查找已卸载的包名称

java - 在 kotlin 中操作时间的最佳方式是什么?

android - 如果我已经使用 AsyncTask,如何避免主线程上的网络异常?

java - GC 期间释放了什么?

ios - 如何使用 Swift 构建 ScrollView?

java - Android 应用 NPE : Unable to invoke virtual method setOnClickListener on null object

javascript - 在 React Native 中,如何获取自定义组件的 Y 偏移量,然后将 ScrollView 滚动到它的位置?

android - 滚动+点击后 ScrollView 停止工作

ios - TouchableOpacity onPress在ScrollView中不起作用

当只有一个直接 child 时,Java 返回 "ScrollView can host only one direct child"