2017-03-22 49 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