我只是像这样添加一个 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/