简体   繁体   English

如何在LinearLayout中获取视图坐标

[英]How to get coordinate of view in LinearLayout

I have a LinearLayout which can have many child views. 我有一个LinearLayout ,可以有很多子视图。 Child views are added programatically. 子视图以编程方式添加。

                                                      i need this distance
                                                    <---------------------->
--------------------------------wrapper ln----------------------------------
-                                             ...  cv4   cv3    cv2    cv1 -  
----------------------------------------------------------------------------

Wrapper ln gravity is setted to right, so every added child view putting right. 包装器ln的引力设置为正确,因此每个添加的子视图均正确放置。

For example when I click to "cv4" i need to get the distance from the right side of wrapper linear layout. 例如,当我单击“ cv4”时,我需要从包装线性布局的右侧获取距离。

Child views are of LinearLayout type. 子视图为LinearLayout类型。

You can call getWidth() on the parent LinearLayout and getLeft() on the dynamically added view to get the position from the right: 您可以在父级LinearLayout上调用getWidth() ,在动态添加的视图上调用getLeft() ,以从右侧获取位置:

parent.getWidth() - c4.getLeft();

You can also hook onto a OnGlobalLayoutListener so you can get the co-ordinates every time it changes its dimensions: 您还可以连接到OnGlobalLayoutListener以便每次更改尺寸时都可以获取坐标:

parent.getViewTreeObserver().addOnGlobalLayoutListener(
     new ViewTreeObserver.OnGlobalLayoutListener() {
          public void onGlobalLayout() {
               int right = parent.getWidth() - c4.getLeft();
          }
     }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM