小编Mar*_*gro的帖子

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

我在 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 …
Run Code Online (Sandbox Code Playgroud)

android

5
推荐指数
1
解决办法
2576
查看次数

标签 统计

android ×1