2017-03-22 106 views
0

的孩子我需要一个视图(自定义视图称为DayView)绵延两次滚动视图的高度,它在,这种观点认为,有两个嵌套的意见,其中之一是另一个自定义视图。自定义视图全部从RelativeLayout延伸。如何正确调整customview

的布局是这样的:

<ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <!-- Is twice the size as scrollView, see code below --> 
    <com.example.DayView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <!-- Should be same size as parent DayView --> 
     <include layout="@layout/layout_dayplan_background" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"/> 

     <!-- Should be same size as parent DayView --> 
     <com.example.BlockView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"/> 
    </com.example.DayView> 
</ScrollView> 

两个嵌套视图(includeBlockView)应当具有相同的高度作为父DayView

DayView,我重写onMeasure使其两倍。这工作:(见“* 2”在倒数第二排)

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    int parentWidth = MeasureSpec.getSize(widthMeasureSpec); 
    int parentHeight = MeasureSpec.getSize(heightMeasureSpec); 
    this.setMeasuredDimension(parentWidth, parentHeight); 
    this.setLayoutParams(new ScrollView.LayoutParams(parentWidth, parentHeight * 2)); 
    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
} 

DayView有正确的尺寸,但是,这两个嵌套视图不与父母一起STRETCH时,但似乎使用一个wrap_content高度布局规则。

问题

我怎样才能让这两个嵌套视图接管相同的高度父DayView

+0

尝试删除此行后'super.onMeasure(widthMeasureSpec,heightMeasureSpec);' –

+0

@JimitPatel:这不起作用,窗口似乎不再是尺寸的两倍,而且孩子们也没有一点都不画。 – Marcel50506

+0

然后生成'parentHeight * 2'并将其传递给'setMeasuredDimension()'方法。随处使用修改后的值。另外,删除'setLayoutParams()'和'super.onMeasure()'。因为我一般用'setMeasuredDimension()'没有其他提到的方法方法,它为我的作品 –

回答

0

你可以试试下面的方法

 int parentWidth = MeasureSpec.getSize(widthMeasureSpec); 
     int parentHeight = MeasureSpec.getSize(heightMeasureSpec) * 2; 
     this.setMeasuredDimension(parentWidth, parentHeight); 
     measureChildren(MeasureSpec.makeMeasureSpec(parentWidth, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(parentHeight, MeasureSpec.EXACTLY)); 
     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 

这是测量孩子了。也许这可以工作

+0

这不起作用。结果是DayView的行为像高度是'wrap_content',所以它不再是滚动窗口大小的两倍。 – Marcel50506