view.getlLeft()、view.getBottom()、view.getRight()和view.getBottom()

本文通过一个具体的示例详细解释了Android中view.getTop(), view.getLeft(), view.getRight(), 和view.getBottom()方法返回的坐标值是相对于父控件而非屏幕坐标系的概念。通过代码示例展示了如何获取这些坐标值,并分析了它们的实际意义。

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

view.getlLeft()、view.getBottom()、view.getRight()和view.getBottom()所获得的值不是控件相对于手机屏幕而言的,

而是控件相对于父控件而言的。

下面用一个demo来说明上面四个方法是相对于父控件而言的。我的测试手机的分辨率是 480 * 800。1dp = 1.5px。

1. activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.example.zhengjunchen.vsgdemo.MainActivity">

    <FrameLayout
        android:id="@+id/fl"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:background="#aac864c8"
        android:layout_width="200dp"
        android:layout_height="200dp">

        <ImageView
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:id="@+id/iv"
            android:background="#aa00ff00"
            android:layout_width="100dp"
            android:layout_height="100dp"/>

    </FrameLayout>

</RelativeLayout>


布局效果图如下所示:




2. MainActivity.java

package com.example.zhengjunchen.vsgdemo;

import android.bluetooth.BluetoothAdapter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {


    private FrameLayout frameLayout;
    private ImageView iv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        frameLayout = (FrameLayout) findViewById(R.id.fl);

        iv = (ImageView) findViewById(R.id.iv);

        //view加载完成时回调
        frameLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                // TODO Auto-generated method stub
                Log.e("TAG", "frameLayout.getLeft(): " + frameLayout.getLeft());
                Log.e("TAG","frameLayout.getTop(): " + frameLayout.getTop());
                Log.e("TAG","frameLayout.getRight(): " + frameLayout.getRight());
                Log.e("TAG","frameLayout.getBottom(): " + frameLayout.getBottom());

                Log.e("TAG","--------------------------------------------------");

                Log.e("TAG","iv.getLeft(): " + iv.getLeft());
                Log.e("TAG","iv.getTop(): " + iv.getTop());
                Log.e("TAG","iv.getRight(): " + iv.getRight());
                Log.e("TAG","iv.getBottom(): " + iv.getBottom());
            }
        });

}

3. log的打印日志

11-11 08:25:58.892 4497-4497/com.example.zhengjunchen.vsgdemo E/TAG: frameLayout.getLeft(): 15
11-11 08:25:58.893 4497-4497/com.example.zhengjunchen.vsgdemo E/TAG: frameLayout.getTop(): 15
11-11 08:25:58.893 4497-4497/com.example.zhengjunchen.vsgdemo E/TAG: frameLayout.getRight(): 315
11-11 08:25:58.893 4497-4497/com.example.zhengjunchen.vsgdemo E/TAG: frameLayout.getBottom(): 315
11-11 08:25:58.893 4497-4497/com.example.zhengjunchen.vsgdemo E/TAG: --------------------------------------------------
11-11 08:25:58.893 4497-4497/com.example.zhengjunchen.vsgdemo E/TAG: iv.getLeft(): 15
11-11 08:25:58.894 4497-4497/com.example.zhengjunchen.vsgdemo E/TAG: iv.getTop(): 15
11-11 08:25:58.894 4497-4497/com.example.zhengjunchen.vsgdemo E/TAG: iv.getRight(): 165
11-11 08:25:58.894 4497-4497/com.example.zhengjunchen.vsgdemo E/TAG: iv.getBottom(): 165

分析:

由于手机的分辨率是480 * 800 的,所以 10dp = 15px。 从log打印的日志可以看出 如果imageview 的坐标不是相对于父控件framelayout而言的话,那么打印的坐标值不会是 15px。

 iv.getBottom() = iv.getTop() + iv.getMeasuredHeight();

iv.getRight() = iv.getLeft() + iv.getMeasuredWidth();


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值