2017-09-27 64 views
0

我有一个getLeft()和getTop()的问题,它返回0.我在Stackoverflow上看到我应该使用GlobaleLayoutListener,但仍然不工作。按钮getLeft()和getTop()返回0

我的看法正在以这种方式工作。有一个第一个水平滚动视图栏让用户选择用户想要查看他的图表的时间段。当他选择“每周”或“每月”时,会出现第二个小节。用户可以选择一个月或一天。所以在开始时第二个酒吧的能见度消失了。

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

我试过:

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 
     } 
    }); 

但它不工作:/

有我的布局:

<?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> 

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


    <WebView 
     android:id="@+id/web_view_wb_quick_view_consumption" 
     android:layout_width="match_parent" 
     android:layout_height="400px" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentStart="true" 
     android:layout_below="@id/test" 
     /> 
</RelativeLayout> 

回答

1

View documentation,讨论方法来检索位置:

这些方法都返回视图的位置相对于其父。例如,当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> 

为了让您的Button的绝对位置的窗口中,用OnGlobalLayoutListener这里面代码:

int[] coords = new int[2]; 
decButton.getLocationInWindow(coords); 
int absoluteX = coords[0]; 
int absoluteY = coords[1]; 
+0

谢谢它的工作! –

0

这可能是因为该方法是在视图被测量之前调用。我不知道GlobalLayoutListener是如何工作的。相反,你可以试试下面的代码得到它的工作适合你,如果没有其他作品

new Handler().postDelayed(new Runnable() { 
    @Override 
    public void run() { 
     decButton.getLeft() // This method will run after 200ms. 
    } 
}, 200); 
0

这是因为鉴于可能没有被你查询视图的X位置的时间膨胀。你可以尝试这样的事情。

decButton.post(new Runnable(){ 
    public void run(){ 
     float leftX = decButton.getLeft(); 
     float topY = decButoon.getTop(); 
     System.out.println(leftX + " " + topY); 
    } 
}); 

此外,如果你尝试在视图或其任何方兴未艾的父母已经去得到一个视图的位置,你会得到0