Android如何獲取子View的位置及座標詳解
一、View
1.1、View 概述
檢視 (View) 是一個容器,專門負責佈局。表現為顯示在螢幕上的各種檢視,如 TextView、LinearLayout 等。
1.2、View 分類
View 主要分為兩類,具體如下表格所示:
類別 | 示例 | 特點 |
---|---|---|
單一檢視 | 即一個 View,如 TextView、EditText | 不包含子View |
檢視組 | 即多個 View 組成的 ViewGroup,如 RelativeLayout | 包含子View |
1.3、View 類簡介
View 類是 Android 中各種元件的基類;
View 的建構函式有四個,具體如下所示:
public View(Context context) { } public View(Context context,@Nullable AttributeSet attrs) { this(context,attrs,0); } public View(Context context,@Nullable AttributeSet attrs,int defStyleAttr) { this(context,defStyleAttr,0); } public View(Context context,int defStyleAttr,int defStyleRes) { }
原始碼中 View 的建構函式
通過原始碼的註釋我們可以看出:
- 如果 View 是在 Java 程式碼裡面 new 的,則呼叫第一個建構函式-->View(Context);
- 如果 View 是在 xml 裡宣告的,則呼叫第二個建構函式-->View(Context,AttributeSet)。
二、Android 座標系
Android 座標系和數學上的座標系是不一樣的,定義如下:
- 螢幕的左上角為座標原點。
- 向右為 x 軸增大方向。
- 向下為 y 軸增大方向。
具體如下圖所示:
三、View 的位置
View 的位置是相對於父控制元件而言的,由 4 個頂點確定,如下圖 A、B、C、D 所示:
確定 View 的位置有四個引數,分別是 Top、Bottom、Left、Right:
- Top:子 View 左上角距父 View 頂部的距離。
- Left:子 View 左上角距父 View 左側的距離。
- Bottom:子 View 右下角距父 View 頂部的距離。
- Right:子 View 右下角距父 View 左側的距離
具體如下圖所示:
四、獲取 View 位置的方式
View 的位置是通過 getTop()、getLeft()、getBottom()、getRight() 函式進行獲取的。
這裡我寫了一個小例子來演示這四個方法,如下所示:(獲取內部子 View 的位置)
因為是為了演示 View 的位置,所有我這裡用絕對佈局,並且大小的單位都是用 px,具體佈局如下所示:
<?xml version="1.0" encoding="utf-8"?> <AbsoluteLayout 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" tools:context=".MainActivity"> <RelativeLayout android:id="@+id/rl_1" android:layout_width="600px" android:layout_height="600px" android:layout_x="200px" android:layout_y="200px" android:background="@color/colorPrimaryDark"> <View android:id="@+id/view" android:layout_width="300px" android:layout_height="300px" android:layout_centerInParent="true" android:background="@color/colorAccent" /> </RelativeLayout> </AbsoluteLayout>
我們現在用四個方法來獲取一下 View 的位置,具體程式碼如下所示:
public class CoordinateActivity extends AppCompatActivity { private View mView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_coordinate); rl1 = findViewById(R.id.rl_1); mView = findViewById(R.id.view); } @Override protected void onResume() { super.onResume(); new Handler().postDelayed(new Runnable() { @Override public void run() { MyLogUtils.i(mView.getTop() + "--Top --mView"); MyLogUtils.i(mView.getBottom() + "--Bottom --mView"); MyLogUtils.i(mView.getLeft() + "--Left --mView"); MyLogUtils.i(mView.getRight() + "--Right --mView"); MyLogUtils.i(mView.getX() + "--X --mView"); MyLogUtils.i(mView.getY() + "--Y --mView"); } },200); } }
列印結果如下所示:
最外層紫色的 View 的座標是(200,200),大小是 600px,在它內部,有一個大小為 300px 的子 View 位於其中心位置,所以上述列印結果是完全正確的。
注意:
- 我這裡呼叫 getTop() 等方法是在 onResume() 裡面,並且延時了 200ms,是因為如果不延遲直接呼叫,會出現 View 還沒有繪製完,所以獲取到的位置都是 0,所以就用最簡單的延遲處理了一下(這裡的處理方法有很多,比如 View.post() 等);
- getX() 和 getY() 的意思是獲取子 View 相對父容器的座標,所以這裡結果都是 150。
總結
到此這篇關於Android如何獲取子View的位置及座標的文章就介紹到這了,更多相關Android獲取子View位置及座標內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!