android view getLeft(), getRight(), getTop(), getBottom()等相对位置 与getGlobalVisibleRect(Rect r)等绝对位置


View的getTop()getBottom()getLeft()getRight()获取的都是当前View相对于它的父类容器的顶部、底部、左边和右边的位置是相对位置

看下图:




   right = left + width;

   bottom = top + height;

再来看一看getHeight(),getWidth(),他们是这样的



总结(正解):
 getWidth()/getHeight: View在设定好布局后整个View的宽度/高度。
 getMeasuredWidth(): 对View上的内容进行测量后得到的View内容佔据的宽度,前提是你必须在父布局的onLayout()方法或者此View的onDraw()方法裡调 用measure(0,0);(measure 参数的值你可以自己定义),否则你得到的结果和getWidth()得到的结果一样。





那如何获取View的绝对位置呢?

http://www.cnblogs.com/mengdd/p/3273284.html

getGlobalVisibleRect(Rect r)是可以得到绝对坐标的。



getLocationInWindow(int[] location) 返回View左上角的绝对坐标


getLocationOnScreen(int[] location) 返回View左上角的绝对坐标

这两个方法目前还不出有什么差别。

直接看代码 




    private String getViewInfo(View view) {
        StringBuffer stringBuffer = new StringBuffer();


        int top = view.getTop();
        int left = view.getLeft();
        int right = view.getRight();
        int bottom = view.getBottom();
        int width = view.getWidth();
        int height = view.getHeight();


        stringBuffer.append("Info relative to Parent: " + "left: " + left
                + ", right: " + right + ", top: " + top + ", bottom: " + bottom
                + ", width: " + width + ", height: " + height);


        // API Level 11
        stringBuffer.append("\n view.getTranslationX(): "
                + view.getTranslationX());
        stringBuffer.append("\n view.getTranslationY(): "
                + view.getTranslationY());


        Rect rect = new Rect();
        view.getLocalVisibleRect(rect);
        stringBuffer.append("\ngetLocalVisibleRect(): ");
        stringBuffer.append("\n " + rect.toString());


        Rect globalRect = new Rect();
        view.getGlobalVisibleRect(globalRect);
        stringBuffer.append("\ngetGlobalVisibleRect(): ");
        stringBuffer.append("\n " + globalRect.toString());


        int[] locationInWindow = new int[2];
        view.getLocationInWindow(locationInWindow);
        stringBuffer.append("\ngetLocationInWindow(): ");
        stringBuffer.append("\n " + locationInWindow[0] + ", "
                + locationInWindow[1]);


        int[] locationOnScreen = new int[2];
        view.getLocationOnScreen(locationOnScreen);
        stringBuffer.append("\ngetLocationOnScreen(): ");
        stringBuffer.append("\n " + locationOnScreen[0] + ", "
                + locationOnScreen[1]);


        return stringBuffer.toString();
   


}















要为 Android GridLayout 添加分割线,可以通过创建自定义的 GridItemDecoration 类来实现。这个类需要继承自 RecyclerView.ItemDecoration,然后重写其中的 onDraw() 和 getItemOffsets() 方法。 下面是一个示例代码,可以为 GridLayout 添加水平和垂直方向的分割线: ```java public class GridItemDecoration extends RecyclerView.ItemDecoration { private int horizontalSpacing; private int verticalSpacing; private Paint dividerPaint; public GridItemDecoration(int horizontalSpacing, int verticalSpacing, int dividerColor) { this.horizontalSpacing = horizontalSpacing; this.verticalSpacing = verticalSpacing; dividerPaint = new Paint(); dividerPaint.setColor(dividerColor); dividerPaint.setStyle(Paint.Style.FILL); } @Override public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) { int childCount = parent.getChildCount(); int rowCount = parent.getAdapter().getItemCount() / parent.getChildCount(); int lastRowChildCount = parent.getAdapter().getItemCount() % parent.getChildCount(); for (int i = 0; i < childCount; i++) { View child = parent.getChildAt(i); int childAdapterPosition = parent.getChildAdapterPosition(child); int row = childAdapterPosition / rowCount; int column = childAdapterPosition % rowCount; if (row != 0) { // 绘制垂直方向的分割线 int left = child.getLeft() - horizontalSpacing / 2; int right = child.getRight() + horizontalSpacing / 2; int top = child.getTop() - verticalSpacing; int bottom = child.getTop(); c.drawRect(left, top, right, bottom, dividerPaint); } if (column != 0) { // 绘制水平方向的分割线 int left = child.getLeft() - horizontalSpacing; int right = child.getLeft(); int top = child.getTop() - verticalSpacing / 2; int bottom = child.getBottom() + verticalSpacing / 2; c.drawRect(left, top, right, bottom, dividerPaint); } // 绘制右边和底部的边框 if (column == rowCount - 1 && lastRowChildCount == 0) { int left = child.getRight() - horizontalSpacing / 2; int right = child.getRight() + horizontalSpacing / 2; int top = child.getTop() - verticalSpacing; int bottom = child.getBottom() + verticalSpacing; c.drawRect(left, top, right, bottom, dividerPaint); } if (row == parent.getAdapter().getItemCount() / rowCount - 1) { int left = child.getLeft() - horizontalSpacing; int right = child.getRight() + horizontalSpacing; int top = child.getBottom() - verticalSpacing / 2; int bottom = child.getBottom() + verticalSpacing / 2; c.drawRect(left, top, right, bottom, dividerPaint); } } } @Override public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { outRect.set(horizontalSpacing, verticalSpacing, horizontalSpacing, verticalSpacing); } } ``` 使用方式: ```java GridLayout gridLayout = findViewById(R.id.grid_layout); gridLayout.addItemDecoration(new GridItemDecoration(16, 16, Color.GRAY)); ``` 其中,16 表示水平和垂直方向的间距,Color.GRAY 表示分割线的颜色。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Android西红柿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值