簡體   English   中英

獲取相對於屏幕頂部的textview和imageview的最終位置

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

我有一個位圖,下面是一個時間線。

例如,考慮在右側的布局

所有底部時間線(1、2、3 ...)都與頂部處於同一高度。

時間軸是一個文本視圖,它具有固定的布局高度和寬度,正如在xml中定義的一樣,例如時間軸1定義為:

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

但是,位圖的高度和寬度可以通過編程方式進行更改。

因此,在某些情況下,位圖的高度增加到足以重疊時間線。 換句話說,位圖的垂直位置相對於時間線的垂直位置增加。

我想得到:

1)位圖相對於屏幕頂部的最終垂直位置。

2)時間軸相對於屏幕頂部的最終垂直位置。

我嘗試執行以下操作:

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.

從代碼中可以看出, getHeight()getBottom()這兩種方法都將高度返回為0。

如何獲得兩者相對於單元格顯示屏頂部的高度(查看終點位置)?

希望這可以幫助

@Override

受保護的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);

}

這是可以做到的:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM