按钮 getLeft() 和 getTop() 返回 0

Mar*_*gro 5 android

我在 getLeft() 和 getTop() 方面遇到问题,它返回 0。我在 Stackoverflow 上看到我应该使用 GlobaleLayoutListener 但仍然无法正常工作。

我的观点就是这样运作的。第一个水平滚动视图栏可让用户选择想要查看图表的时间段。当他选择“每周”或“每月”时,会出现第二个栏。用户可以选择一个月或一天。所以一开始第二个条的可见性就消失了。

我想设置第二栏的项目。因为像今天一样,我们是在九月,如果用户单击“每月”,我希望第二个栏的第一项是“九月”。

我尝试过:

ViewTreeObserver observer = decButton.getViewTreeObserver();
    observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            System.out.println("KIRBY " + decButton.getLeft());
            //in here, place the code that requires you to know the dimensions.
            //Place your code here
        }
    });
Run Code Online (Sandbox Code Playgroud)

但它不起作用:/

有我的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"

android:orientation="vertical"
android:background="@color/white"
>
<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

</RelativeLayout>
<android.support.v7.widget.Toolbar
    android:id="@+id/quickViewConsumptionToolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/wb_color"
    android:titleTextColor="@color/white"
    android:theme="@style/toolbar_white"/>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/hello">
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/test">
        <HorizontalScrollView
            android:id="@+id/horizontalBar"
            android:layout_width="match_parent"
            android:layout_height="100px">
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
                <RelativeLayout
                    android:layout_width="90dp"
                    android:layout_height="wrap_content">
                    <Button
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/dailyButton"
                        android:text="Daily"
                        android:textAllCaps="false"
                        android:textStyle="normal"
                        android:layout_marginRight="5px"
                        android:background="@android:color/transparent"/>

                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="90dp"
                    android:layout_height="wrap_content">
                    <Button
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/weeklyButton"
                        android:text="Weekly"
                        android:textAllCaps="false"
                        android:textStyle="normal"
                        android:layout_marginRight="5px"
                        android:background="@android:color/transparent"/>

                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="90dp"
                    android:layout_height="wrap_content">
                    <Button
                        android:id="@+id/monthlyButton"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginRight="5px"
                        android:background="@android:color/transparent"
                        android:text="Monthly"
                        android:textAllCaps="false"
                        android:textStyle="normal" />

                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="90dp"
                    android:layout_height="wrap_content">
                    <Button
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/yearlyButton"
                        android:text="Yearly"
                        android:textAllCaps="false"
                        android:textStyle="normal"
                        android:layout_marginRight="5px"
                        android:background="@android:color/transparent"/>

                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="90dp"
                    android:layout_height="wrap_content">
                    <Button
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/allTimeButton"
                        android:text="All time"
                        android:textAllCaps="false"
                        android:textStyle="normal"
                        android:layout_marginRight="5px"
                        android:background="@android:color/transparent"/>

                </RelativeLayout>
            </LinearLayout>
        </HorizontalScrollView>

        <View
            android:id="@+id/divider"
            android:layout_width="100dp"
            android:layout_marginLeft="140dp"
            android:layout_marginRight="20dp"
            android:layout_height="1dp"
            android:background="@android:color/black"
            android:layout_below="@+id/horizontalBar"
            android:visibility="visible"
            android:layout_marginBottom="5px"/>

        <HorizontalScrollView
            android:id="@+id/horizontalMonthlyBarBellow"
            android:layout_width="match_parent"
            android:layout_height="100px"
            android:layout_below="@+id/divider"
            android:layout_alignParentStart="true"
            android:visibility="gone">
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
                <RelativeLayout
                    android:layout_width="90dp"
                    android:layout_height="wrap_content">
                    <Button
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/janButton"
                        android:text="January"
                        android:textAllCaps="false"
                        android:textStyle="normal"
                        android:layout_marginRight="5px"
                        android:background="@android:color/transparent"/>

                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="90dp"
                    android:layout_height="wrap_content">
                    <Button
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/febButton"
                        android:text="February"
                        android:textAllCaps="false"
                        android:textStyle="normal"
                        android:layout_marginRight="5px"
                        android:background="@android:color/transparent"/>

                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="90dp"
                    android:layout_height="wrap_content">
                    <Button
                        android:id="@+id/marchButton"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginRight="5px"
                        android:background="@android:color/transparent"
                        android:text="March"
                        android:textAllCaps="false"
                        android:textStyle="normal" />

                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="90dp"
                    android:layout_height="wrap_content">
                    <Button
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/aprilButton"
                        android:text="April"
                        android:textAllCaps="false"
                        android:textStyle="normal"
                        android:layout_marginRight="5px"
                        android:background="@android:color/transparent"/>

                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="90dp"
                    android:layout_height="wrap_content">
                    <Button
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/mayButton"
                        android:text="May"
                        android:textAllCaps="false"
                        android:textStyle="normal"
                        android:layout_marginRight="5px"
                        android:background="@android:color/transparent"/>

                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="90dp"
                    android:layout_height="wrap_content">
                    <Button
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/juneButton"
                        android:text="June"
                        android:textAllCaps="false"
                        android:textStyle="normal"
                        android:layout_marginRight="5px"
                        android:background="@android:color/transparent"/>

                </RelativeLayout>
                <RelativeLayout
                    android:layout_width="90dp"
                    android:layout_height="wrap_content">
                    <Button
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/julyButton"
                        android:text="July"
                        android:textAllCaps="false"
                        android:textStyle="normal"
                        android:layout_marginRight="5px"
                        android:background="@android:color/transparent"/>

                </RelativeLayout>
                <RelativeLayout
                    android:layout_width="90dp"
                    android:layout_height="wrap_content">
                    <Button
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/augButton"
                        android:text="August"
                        android:textAllCaps="false"
                        android:textStyle="normal"
                        android:layout_marginRight="5px"
                        android:background="@android:color/transparent"/>

                </RelativeLayout>
                <RelativeLayout
                    android:id="@+id/septView"
                    android:layout_width="90dp"
                    android:layout_height="wrap_content">
                    <Button
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/sepButton"
                        android:text="September"
                        android:textAllCaps="false"
                        android:textStyle="normal"
                        android:layout_marginRight="5px"
                        android:background="@android:color/transparent"/>

                </RelativeLayout>
                <RelativeLayout
                    android:layout_width="90dp"
                    android:layout_height="wrap_content">
                    <Button
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/octButton"
                        android:text="October"
                        android:textAllCaps="false"
                        android:textStyle="normal"
                        android:layout_marginRight="5px"
                        android:background="@android:color/transparent"/>

                </RelativeLayout>
                <RelativeLayout
                    android:layout_width="90dp"
                    android:layout_height="wrap_content">
                    <Button
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/novButton"
                        android:text="November"
                        android:textAllCaps="false"
                        android:textStyle="normal"
                        android:layout_marginRight="5px"
                        android:background="@android:color/transparent"/>

                </RelativeLayout>
                <RelativeLayout
                    android:layout_width="90dp"
                    android:layout_height="wrap_content">
                    <Button
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/decButton"
                        android:text="December"
                        android:textAllCaps="false"
                        android:textStyle="normal"
                        android:layout_marginRight="5px"
                        android:background="@android:color/transparent"/>
                </RelativeLayout>
            </LinearLayout>
        </HorizontalScrollView>

        <HorizontalScrollView
            android:id="@+id/horizontalBarWeeklyBellow"
            android:layout_width="match_parent"
            android:layout_height="100px"
            android:layout_below="@+id/divider"
            android:layout_alignParentStart="true"
            android:visibility="gone">
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
                <RelativeLayout
                    android:layout_width="90dp"
                    android:layout_height="wrap_content">
                    <Button
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/mondayButton"
                        android:text="Monday"
                        android:textAllCaps="false"
                        android:textStyle="normal"
                        android:layout_marginRight="5px"
                        android:background="@android:color/transparent"/>

                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="90dp"
                    android:layout_height="wrap_content">
                    <Button
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/tuesdayButton"
                        android:text="Tuesday"
                        android:textAllCaps="false"
                        android:textStyle="normal"
                        android:layout_marginRight="5px"
                        android:background="@android:color/transparent"/>

                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="90dp"
                    android:layout_height="wrap_content">
                    <Button
                        android:id="@+id/wednesdayButton"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginRight="5px"
                        android:background="@android:color/transparent"
                        android:text="Wednesday"
                        android:textAllCaps="false"
                        android:textStyle="normal" />

                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="90dp"
                    android:layout_height="wrap_content">
                    <Button
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/thursdayButton"
                        android:text="Thursday"
                        android:textAllCaps="false"
                        android:textStyle="normal"
                        android:layout_marginRight="5px"
                        android:background="@android:color/transparent"/>

                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="90dp"
                    android:layout_height="wrap_content">
                    <Button
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/fridayButton"
                        android:text="Friday"
                        android:textAllCaps="false"
                        android:textStyle="normal"
                        android:layout_marginRight="5px"
                        android:background="@android:color/transparent"/>

                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="90dp"
                    android:layout_height="wrap_content">
                    <Button
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/saturdayButton"
                        android:text="Saturday"
                        android:textAllCaps="false"
                        android:textStyle="normal"
                        android:layout_marginRight="5px"
                        android:background="@android:color/transparent"/>

                </RelativeLayout>
                <RelativeLayout
                    android:layout_width="90dp"
                    android:layout_height="wrap_content">
                    <Button
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/sundayButton"
                        android:text="Sunday"
                        android:textAllCaps="false"
                        android:textStyle="normal"
                        android:layout_marginRight="5px"
                        android:background="@android:color/transparent"/>

                </RelativeLayout>

            </

Ben*_* P. 7

View文档中,讨论检索位置的方法:

这些方法都返回视图相对于其父视图的位置。例如,当getLeft()返回 20 时,这意味着视图位于其直接父级左边缘右侧 20 像素处。

OnGlobalLayoutListener该技术对您“不起作用”的原因是您的内容decButton被包裹在 中,与左上角RelativeLayout没有间距(边距/填充) 。RelativeLayout

<RelativeLayout
    android:layout_width="90dp"
    android:layout_height="wrap_content">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/decButton"
        android:text="December"
        android:textAllCaps="false"
        android:textStyle="normal"
        android:layout_marginRight="5px"
        android:background="@android:color/transparent"/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

Button要获取您在窗口中的绝对位置,请在您的 中使用以下代码OnGlobalLayoutListener

int[] coords = new int[2];
decButton.getLocationInWindow(coords);
int absoluteX = coords[0];
int absoluteY = coords[1];
Run Code Online (Sandbox Code Playgroud)