getLeft,getTop,getRight,getBottm区别

本文详细解释了Android中视图(View)的getLeft, getTop, getRight, getBottom及getWidth等方法的含义,并通过实例展示了这些方法的具体用法。特别强调了这些方法返回的是视图左上角相对于父布局的像素位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载请标明出处:
http://blog.youkuaiyun.com/zq2114522/article/details/51112281
本文出自:【梁大盛的博客】

getLeft,getTop,getRight,getBottm傻傻分不清.
通过下面图片已经很清晰的把该说的说明白了!

这里写图片描述

注意:
    1.getLeft,getTop,getRight,getBottm,getWidth是View左上角的锚点相对父布局的像素.
    2.可以看到父布局在布局文件里面设置了android:padding="20dp".因为density=1.5实质转换为像素应该是30px.

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res/com.example.dsliang.viewdemo"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="20dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/GREEN"
        android:gravity="center"
        android:orientation="vertical"
        android:padding="20dp">

        <com.example.dsliang.viewdemo.ViewDemo
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/GRAY" />
    </LinearLayout>
</LinearLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值