Android 滚动RelativeLayout-部分内容上的白色边框

Android 滚动RelativeLayout-部分内容上的白色边框,android,android-layout,Android,Android Layout,我有一个相当简单的片段,它将一些彩色图像视图添加到一个相对的视图中。屏幕上的图像太多了,所以我实现了一些自定义滚动 然而,当我四处滚动时,我看到在滚动前屏幕边缘处有一个大约90dp的白色边框重叠部分。 很明显,ImageView仍在正确地创建和绘制,但它们被掩盖了 我怎样才能摆脱这个 我试过: 更改RelativeLayout和FrameLayout以包装内容、填充父项、匹配父项以及这些内容的一些组合 将两个布局的填充和边距设置为0dp 示例: 片段: public class MyFra

我有一个相当简单的片段,它将一些彩色图像视图添加到一个相对的视图中。屏幕上的图像太多了,所以我实现了一些自定义滚动

然而,当我四处滚动时,我看到在滚动前屏幕边缘处有一个大约90dp的白色边框重叠部分。 很明显,ImageView仍在正确地创建和绘制,但它们被掩盖了

我怎样才能摆脱这个

我试过:

  • 更改RelativeLayout和FrameLayout以包装内容、填充父项、匹配父项以及这些内容的一些组合
  • 将两个布局的填充和边距设置为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; 
    }
}
公共类MyFrag扩展了片段实现OnTouchListener{
int-currentX;
电流;
相对长度容器;
final int[]colors={Color.BLACK,Color.RED,Color.BLUE};
@凌驾
创建视图上的公共视图(布局充气机、视图组框架容器、捆绑包保存状态){
返回充气机。充气(R.layout.fragment_myfrag,空);
}
@凌驾
已创建ActivityState上的公共无效(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
container=(RelativeLayout)getView().findViewById(R.id.container);
container.setOnTouchListener(this);
//临时-添加一组图像以测试滚动
对于(int i=0;i在查看时,我注意到
onMeasure()
调用
applyHorizontalSizeRules(LayoutParams childParams,int myWidth)
applyVerticalSizeRules(LayoutParams childParams,int myHeight)

ApplyHorizontalSizeroles
中,我发现对于myWidth和myHeight参数:

// -1 indicated a "soft requirement" in that direction. For example:        
// left=10, right=-1 means the view must start at 10, but can go as far as it wants to the right
myWidth参数初始化为-1,然后根据onMeasure()参数的MeasureSpec模式进行更改

因此,我创建了自己的视图,扩展了RelativeLayout,并重写了onMeasure(),将模式设置为“unspecified”:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int width = MeasureSpec.getSize(widthMeasureSpec);
    int height = MeasureSpec.getSize(heightMeasureSpec);

    int newWidthSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.UNSPECIFIED);
    int newHeightSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.UNSPECIFIED);

    super.onMeasure(newWidthSpec, newHeightSpec);
}
工作起来很有魅力!

在浏览时,我注意到
onMeasure()
调用了
applyHorizontalSizeRules(LayoutParams childParams,int myWidth)
applyVerticalSizeRules(LayoutParams childParams,int myHeight)

ApplyHorizontalSizeroles
中,我发现对于myWidth和myHeight参数:

// -1 indicated a "soft requirement" in that direction. For example:        
// left=10, right=-1 means the view must start at 10, but can go as far as it wants to the right
myWidth参数初始化为-1,然后根据onMeasure()参数的MeasureSpec模式进行更改

因此,我创建了自己的视图,扩展了RelativeLayout,并重写了onMeasure(),将模式设置为“unspecified”:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int width = MeasureSpec.getSize(widthMeasureSpec);
    int height = MeasureSpec.getSize(heightMeasureSpec);

    int newWidthSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.UNSPECIFIED);
    int newHeightSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.UNSPECIFIED);

    super.onMeasure(newWidthSpec, newHeightSpec);
}
工作起来很有魅力