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

一笑奈何 提交于 2019-12-01 12:16:24

问题


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 same height from top.

The timeline is a textview which has fixed layout height and width as it is defined in xml like timeline 1 is defined as:

<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" />

However the bitmap height and width can vary as it is done programatically.

So in certain cases, the bitmap height increases enough to overlap the timeline. In other words, the vertical position of bitmap increases with respect to the vertical position of the timeline.

I want to get:

1) the ended vertical position of bitmap with respect to top of the screen.

2) the ended vertical position of timeline with respect to top of the screen.

I tried to do the following:

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.

As seen from the code, both the methods, getHeight() and getBottom() are returning height as 0.

How to get the height (view end position) of both with respect to top of the cell display ?


回答1:


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

}




回答2:


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


来源:https://stackoverflow.com/questions/18533224/getting-end-position-of-the-textview-and-imageview-with-respect-to-top-of-the-sc

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