获取的TextView和ImageView的结束位置相对于屏幕上方 [英] Getting end position of the textview and imageview with respect to top of the screen

查看:105
本文介绍了获取的TextView和ImageView的结束位置相对于屏幕上方的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个位图和它下面是一个时间线。

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

例如,考虑的右侧布局<一href="http://stackoverflow.com/questions/17103026/canvas-containing-bitmap-size-proper-adjustment-in-landscape-and-portrait-mode">FIGURE.

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

所有底部时限(1,2,3,...)是在从顶部的高度相同。

All the bottom timelines (1, 2, 3...) are in the same height from top.

时间轴是一个TextView具有固定布局的高度和宽度,因为它是在XML定义 像的时间表1被定义为:

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.

我想:

<强> 1)位图相对于屏幕的顶部的端垂直位置。

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

2)时间表相对于屏幕上方的端垂直位置。

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

我试着做到以下几点:

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.

由于从code看出,这两种方法,的getHeight() getBottom()是返回高度为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 ?

推荐答案

这是它如何可以做到:

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

这篇关于获取的TextView和ImageView的结束位置相对于屏幕上方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆