2017-09-27 32 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