获取 0 TextView 的位置 x,y

Getting 0 the Location x,y of TextView

我在 android 中制作单词拼图。我有一个 table-layout 和 10 行 table-layout 和连续 10 个 textview。我总共有 100 个 textview。我需要获取 textview 的位置。我尝试了 getLeft()-getTop()、getLocationInWindow()、getLocationOnScreen() 但我总是得到 0.

示例:

konumal();

}

    public void konumal() {

        int[] korr = new int[2];

        ((TextView)findViewById(R.id.tablojj)).getLocationInWindow(korr);

        Log.w("Dikkat", "x : " + korr[0] + " y : " + korr[1]);

    }

我该如何解决这个问题?

我需要使用哪个功能?

我需要你的帮助。

你必须等待布局传递来设置位置,例如

final View mainView = findViewById(R.id.id_of_the_main_view);
mainView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        // at this point the views are positionned

        konumal()

        // we don't need to listen anymore
        mainView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
    }
});