getLocationInWindow()和getLocationOnScreen()

1豆胸、概念

先看張圖:


c2cec3fdfc039245c9717fb98494a4c27c1e25bb.png

對(duì)比上圖,看下這兩個(gè)函數(shù)的定義:

  • getLocationInWindow():控件相對(duì)于父窗口(非父布局)的左上角為原點(diǎn)的坐標(biāo)赤套。
  • getLocationOnScreen():控件相對(duì)于屏幕的左上角為原點(diǎn)的坐標(biāo)飘痛。

2、實(shí)例

2.1容握、在頁(yè)面中宣脉,非彈窗窗口中

布局文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dp"
        android:orientation="horizontal"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent">
        <Button
            android:id="@+id/btn"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:text="顯示窗口控件位置"
            android:textSize="20sp" />
        <TextView
            android:id="@+id/tv"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginStart="20dp"
            android:textSize="15sp" />
    </LinearLayout>
</android.support.constraint.ConstraintLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {
    int[] screenLocation = new int[2];
    int[] windowLocation = new int[2];
    private Button button;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fullscreen);
        button = findViewById(R.id.btn);
        final TextView tv_timer = findViewById(R.id.tv);
        button.post(new Runnable() {
            @Override
            public void run() {
                //以屏幕的左上角為原點(diǎn)
                button.getLocationOnScreen(screenLocation);

                //以父窗口(非父布局)的左上角為原點(diǎn)
                button.getLocationInWindow(windowLocation);
                tv_timer.setText("dialog_screenX::" + screenLocation[0] + "   dialog_screenY::" + screenLocation[1] + "\n" +
                        "dialog_windowX::" + windowLocation[0] + "   dialog_windowY::" + windowLocation[1]);
                Log.e("TAG", "screenX::" + screenLocation[0] + "   screenY::" + screenLocation[1]);
                Log.e("TAG", "windowX::" + windowLocation[0] + "   windowY::" + windowLocation[1]);
            }
        });
  }
}

效果如下:


非彈窗中.png

從圖中可以看出,

  • **getLocationInWindow()是相對(duì)于父窗口而非父布局的剔氏;
  • getLocationOnScreen()是相對(duì)于手機(jī)屏幕左上角塑猖;
  • getLocationInWindow()和getLocationOnScreen()在非彈窗中通常情況下是一致的。

2.2谈跛、在彈窗中

布局文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary">

    <Button
        android:id="@+id/btn"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:text="彈窗內(nèi)顯示窗口控件位置"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/tv"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="20dp"
        android:textColor="@android:color/white"
        android:textSize="15sp"
        app:layout_constraintBottom_toBottomOf="@id/btn"
        app:layout_constraintLeft_toRightOf="@id/btn"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

AlertDialog的展示

  AlertDialog.Builder builder = new AlertDialog.Builder(FullScreenActivity.this);
                View contentView = LayoutInflater.from(FullScreenActivity.this).inflate(R.layout.dialog_layout, null);
                final Button button = contentView.findViewById(R.id.btn);
                final TextView tv_timer = contentView.findViewById(R.id.tv);
                button.post(new Runnable() {
                    @Override
                    public void run() {
                        //以屏幕的左上角為原點(diǎn)
                        button.getLocationOnScreen(screenLocation);

                        //以父窗口(非父布局)的左上角為原點(diǎn)
                        button.getLocationInWindow(windowLocation);
                        tv_timer.setText("dialog_screenX::" + screenLocation[0] + "   dialog_screenY::" + screenLocation[1] + "\n" +
                                "dialog_windowX::" + windowLocation[0] + "   dialog_windowY::" + windowLocation[1]);
                        Log.e("TAG", "dialog_screenX::" + screenLocation[0] + "   dialog_screenY::" + screenLocation[1]);
                        Log.e("TAG", "dialog_windowX::" + windowLocation[0] + "   dialog_windowY::" + windowLocation[1]);
                    }
                });
                builder.setView(contentView);
                builder.show();

效果圖


彈窗中.png

從圖中可以看出羊苟,在彈窗中這兩個(gè)方法的含義:

  • getLocationOnScreen()依然是相對(duì)于手機(jī)屏幕左上角;
  • **而getLocationInWindow()是相對(duì)于父窗口的左上角感憾,此時(shí)父窗口不再是頁(yè)面的窗口了蜡励,而是Dialog的窗口,故和getLocationOnScreen()不再一致阻桅;
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末凉倚,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子鳍刷,更是在濱河造成了極大的恐慌占遥,老刑警劉巖俯抖,帶你破解...
    沈念sama閱讀 221,198評(píng)論 6 514
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件输瓜,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡芬萍,警方通過(guò)查閱死者的電腦和手機(jī)尤揣,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,334評(píng)論 3 398
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)柬祠,“玉大人北戏,你說(shuō)我怎么就攤上這事÷祝” “怎么了嗜愈?”我有些...
    開(kāi)封第一講書(shū)人閱讀 167,643評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)莽龟。 經(jīng)常有香客問(wèn)我蠕嫁,道長(zhǎng),這世上最難降的妖魔是什么毯盈? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 59,495評(píng)論 1 296
  • 正文 為了忘掉前任剃毒,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘赘阀。我一直安慰自己益缠,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,502評(píng)論 6 397
  • 文/花漫 我一把揭開(kāi)白布基公。 她就那樣靜靜地躺著幅慌,像睡著了一般。 火紅的嫁衣襯著肌膚如雪酌媒。 梳的紋絲不亂的頭發(fā)上欠痴,一...
    開(kāi)封第一講書(shū)人閱讀 52,156評(píng)論 1 308
  • 那天,我揣著相機(jī)與錄音秒咨,去河邊找鬼喇辽。 笑死,一個(gè)胖子當(dāng)著我的面吹牛雨席,可吹牛的內(nèi)容都是我干的菩咨。 我是一名探鬼主播,決...
    沈念sama閱讀 40,743評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼陡厘,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼抽米!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起糙置,我...
    開(kāi)封第一講書(shū)人閱讀 39,659評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤云茸,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后谤饭,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體标捺,經(jīng)...
    沈念sama閱讀 46,200評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,282評(píng)論 3 340
  • 正文 我和宋清朗相戀三年揉抵,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了亡容。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,424評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡冤今,死狀恐怖闺兢,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情戏罢,我是刑警寧澤屋谭,帶...
    沈念sama閱讀 36,107評(píng)論 5 349
  • 正文 年R本政府宣布,位于F島的核電站龟糕,受9級(jí)特大地震影響桐磁,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜翩蘸,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,789評(píng)論 3 333
  • 文/蒙蒙 一所意、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦扶踊、人聲如沸泄鹏。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,264評(píng)論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)备籽。三九已至,卻和暖如春分井,著一層夾襖步出監(jiān)牢的瞬間车猬,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,390評(píng)論 1 271
  • 我被黑心中介騙來(lái)泰國(guó)打工尺锚, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留珠闰,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,798評(píng)論 3 376
  • 正文 我出身青樓瘫辩,卻偏偏與公主長(zhǎng)得像伏嗜,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子伐厌,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,435評(píng)論 2 359