【发布时间】:2012-10-30 06:10:55
【问题描述】:
我有一个相当简单的 Fragment,它将少量彩色 ImageView 添加到 RelativeLayout。屏幕上无法容纳的图像更多,因此我实现了一些自定义滚动。
但是,当我四处滚动时,我看到在滚动之前屏幕边缘所在的内容右侧有一个大约 90dp 的白色边框重叠部分。 很明显,ImageViews 仍在正确创建和绘制,但它们被掩盖了。
我该如何摆脱它?
我试过了:
- 将 RelativeLayout 和 FrameLayout 更改为 WRAP_CONTENT、FILL_PARENT、MATCH_PARENT 以及它们的一些组合。
- 将两种布局的内边距和边距设置为 0dp。
示例:
片段:
public class MyFrag extends Fragment implements OnTouchListener {
int currentX;
int currentY;
RelativeLayout container;
final int[] colors = {Color.BLACK, Color.RED, Color.BLUE};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup fragContainer, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_myfrag, null);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
container = (RelativeLayout) getView().findViewById(R.id.container);
container.setOnTouchListener(this);
// Temp- Add a bunch of images to test scrolling
for(int i=0; i<1500; i+=100) {
for (int j=0; j<1500; j+=100) {
int color = colors[(i+j)%3];
ImageView image = new ImageView(getActivity());
image.setScaleType(ImageView.ScaleType.CENTER);
image.setBackgroundColor(color);
LayoutParams lp = new RelativeLayout.LayoutParams(100, 100);
lp.setMargins(i, j, 0, 0);
image.setLayoutParams(lp);
container.addView(image);
}
}
}
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
currentX = (int) event.getRawX();
currentY = (int) event.getRawY();
break;
}
case MotionEvent.ACTION_MOVE: {
int x2 = (int) event.getRawX();
int y2 = (int) event.getRawY();
container.scrollBy(currentX - x2 , currentY - y2);
currentX = x2;
currentY = y2;
break;
}
case MotionEvent.ACTION_UP: {
break;
}
}
return true;
}
}
XML:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".FloorPlanFrag">
<RelativeLayout
android:id="@+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</FrameLayout>
【问题讨论】: