Getting end position of the textview and imageview with respect to top of the screen

前端 未结 2 1250
有刺的猬
有刺的猬 2021-01-17 00:19

I have a bitmap and below it is a time line.

As an example consider the right side layout of the FIGURE.

All the bottom timelines (1, 2, 3...) are in the sam

2条回答
  •  心在旅途
    2021-01-17 00:50

    This is how it can be done:

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

提交回复
热议问题