2012-10-29 25 views
1

我有一個相當簡單的Fragment,它將一些彩色ImageView添加到RelativeLayout中。有更多的圖像可以放在屏幕上,所以我實現了一些自定義滾動。滾動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> 

回答

1

,同時期待通過RelativeLayout source,我注意到,onMeasure()電話applyHorizontalSizeRules(LayoutParams childParams, int myWidth)applyVerticalSizeRules(LayoutParams childParams, int myHeight)

applyHorizontalSizeRules我發現對於myWidth和myHeight PARAMS:

// -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,然後改基於所述MeasureSpec的模式onMeasure()的參數。

所以我創建了自己的觀點,即延伸的RelativeLayout,並推翻onMeasure()將模式設置爲「未指定」:

@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); 
} 

就像一個魅力!