2013-08-30 40 views
0

我有一個位圖,下面是一個時間線。獲取textview和imageview相對於屏幕頂部的結束位置

作爲示例,請考慮FIGURE的右側佈局。

所有底部時間軸(1,2,3 ...)都位於頂部相同的高度。

時間軸是已固定,因爲它是在XML 定義等的時間表1佈局的高度和寬度一個TextView定義爲:

<TextView 
android:id="@+id/textView1" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_alignLeft="@+id/HView" 
android:layout_marginLeft="18dp" 
android:layout_marginTop="345dp" 
android:textSize="14sp" 
android:text="1" 
android:textColor="#000000" /> 

然而,因爲它是編程完成的位圖的高度和寬度可以變化。

因此,在某些情況下,位圖高度增加到足以重疊時間軸。換句話說,位圖的垂直位置相對於時間軸的垂直位置增加。

我想:

1)位的端垂直位置相對於屏幕的頂部。

2)時間軸相對於屏幕頂部的結束垂直位置。

我試着做到以下幾點:

TextView bottomTimeLine = (TextView) view.findViewById(R.id.textView1); 

bottomTimeLine.getHeight(); //returns 0. 

bottomTimeLine.getBottom(); //returns 0. 

ImageView img = new ImageView(getActivity()); 

img.setImageDrawable(getResources().getDrawable(R.drawable.disp_bg)); 

img.getHeight(); //returns 0. 

img.getBottom(); //returns 0. 

根據碼看到,這兩種方法,getHeight()getBottom()正在返回高度爲0

如何獲得高度(查看結束位置)對於細胞顯示的頂部?

回答

0

這是怎麼可以做到:

final TextView bottomTimeLine = (TextView) view.findViewById(R.id.textView1); 

final int[] timelineCoord = new int[2]; 

final int[] imgCoord = new int[2]; 



ViewTreeObserver vto = bottomTimeLine.getViewTreeObserver(); 
vto.addOnGlobalLayoutListener((new OnGlobalLayoutListener() { 

    @Override 
    public void onGlobalLayout() { 


     bottomTimeLine.getLocationOnScreen(timelineCoord); 

    Log.d(" bottomTimeLine H ", ""+timelineCoord[1]); 

    timelineHeight = timelineCoord[1]; 
    } 
})); 


ViewTreeObserver vt1 = img.getViewTreeObserver(); 
vt1.addOnGlobalLayoutListener((new OnGlobalLayoutListener() { 

    @Override 
    public void onGlobalLayout() { 


     img.getLocationOnScreen(imgCoord); 

     imgHeight = imgCoord[1] + img.getHeight(); 

    Log.d("Img H ", ""+imgHeight); 

if(imgHeight < timelineHeight) 
{ 
int heightDiff = imgHeight - timelineHeight ; 

heightDiff = heightDiff + 3; 

img.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, heightDiff)); 
} 
    } 
})); 
0

希望這有助於

@覆蓋

保護無效onMeasure(INT widthMeasureSpec,詮釋heightMeasureSpec){ super.onMeasure(widthMeasureSpec,heightMeasureSpec);

int parentWidth = MeasureSpec.getSize(widthMeasureSpec); 
int parentHeight = MeasureSpec.getSize(heightMeasureSpec); 
this.setMeasuredDimension(
     parentWidth/2, parentHeight); 

}

相關問題