【问题标题】:scrollview causes black background to appear滚动视图导致出现黑色背景
【发布时间】:2014-07-04 15:20:44
【问题描述】:

我只是像这样添加一个滚动视图,屏幕加载后会出现黑色背景。

这个黑色区域位于周围 RelativeLayout 的左上角。而滚动视图的定位是android:layout_marginLeft="20dp" android:layout_marginTop="40dp"所以左边20dp和上面40dp是黑色的,而剩下的灰色背景是不受干扰的。

这里是带有滚动视图的 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>

我已经尝试在滚动视图的顶部添加一个 emptyView 以及 2 个 RelativeLayouts。但无论如何,黑色区域不断出现。 (顶部有/没有RelativeLayouts和空视图)

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

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

如何消除滚动视图造成的黑区?

非常感谢!

【问题讨论】:

    标签: android scrollview


    【解决方案1】:

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

          //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);
    

    【讨论】:

    • 我不知道代码示例应该如何帮助您。在我的情况下,解决方案是添加一个滚动视图填充布局,并将您想要的背景颜色设置为布局而不是滚动视图。上面的代码未经测试,但也许会有所帮助。
    猜你喜欢
    • 1970-01-01
    • 2015-11-14
    • 1970-01-01
    • 1970-01-01
    • 2013-01-03
    • 1970-01-01
    • 2015-06-09
    • 1970-01-01
    • 2014-02-20
    相关资源
    最近更新 更多