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

我怕爱的太早我们不能终老 提交于 2019-12-01 14:33:07

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

}

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));
}
    }
}));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!