Android:ScrollView的儿童自定义视图没有达到合适的高度

时间:2015-08-18 14:02:13

标签: android android-custom-view android-scrollview

我正在撰写自定义视图,其中ScrollView为父级,因为自定义视图高度超出了屏幕的高度。我面临的问题是我无法滚动布局。

在活动的用户界面中添加的片段

public class FragmentHour extends Fragment {
   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstance) {
      HourView view = new HourView(getActivity());

      ScrollView sv = new ScrollView(getActivity());
      sv.setFillViewport(true);
      sv.addView(view);

      return sv;
   }
}

自定义视图:

public class HourView extends View {

   // Default rectangle for one row, used to know the sizes
   private Rect mDefRect;

   // Paint to draw the lines
   private Paint mPaint;

   public HourView(Context context) {
      super(context);
      init();
   }

   public HourView(Context context, AttributeSet set) {
      super(context, set);
      init();
   }

   public HourView(Context context, AttributeSet set, int defStyle) {
      super(context, set, defStyle);
      init();
   }

   private void init() {
      mPaint = new Paint();
      mPaint.setColor(Color.GRAY);
      mPaint.setStrokeWidth(UIUtils.dpToPx(getContext(), 0.5f));
   }

   @Override
   protected void onSizeChanged(int w, int h, int oldw, int oldh) {
      super.onSizeChanged(w, h, oldw, oldh);

      // Create a default rectangle, for one row
      // We need to recreate the rectangle multiple times, because getWidth() == 0
      int height = UIUitls.dpToPx(getContext(), 50); // dpi to px
      mDefRect = new Rect(0, 0, getWidth(), height);
   }

   @Override
   protected void onDraw(Canvas canvas) {
      super.onDraw(canvas);

      canvas.drawColor(Color.WHITE);

      int verticalOffset = 0;

      for (int i = 0; i < 24; i++) {
         int cellHeight = mDefRect.height();
         verticalOffset += cellHeight;

         canvas.drawLine(0, verticalOffset, getWidth(), verticalOffset, mPaint);
      }
   }
}

上面的代码将导致:

enter image description here

正如我所说,问题在于我无法滚动视图,可能是因为儿童视图的高度,它是其父母的大小。

我试图覆盖onMeasure方法:

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (mDefRect != null) {
            int measuredWidth = MeasureSpec.getSize(widthMeasureSpec);
            int newHeight = mDefRect.height() * 24;

            setMeasuredDimension(measuredWidth, newHeight);
            // super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(newHeight, MeasureSpec.EXACTLY));
        }
        else {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }

我尝试注册布局更改,然后更改布局参数:

private void init() {
    // ...

    post(new Runnable() {
       @Override
       public void run() {
           getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
               @SuppressLint("NewApi")
               @SuppressWarnings("deprecation")
               @Override
               public void onGlobalLayout() {

                   if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
                        getViewTreeObserver().removeGlobalOnLayoutListener(this);
                        } else {
                            getViewTreeObserver().removeOnGlobalLayoutListener(this);
                      }

                      getLayoutParams().height = mDefRect.height() * 24;
                      // postInvalidate();
                }
            });
        }
    });
}

0 个答案:

没有答案