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

前端 未结 2 1249
有刺的猬
有刺的猬 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:45

    Hope this helps

    @Override

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec);

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

    }

    0 讨论(0)
  • 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));
    }
        }
    }));
    
    0 讨论(0)
提交回复
热议问题