4

我使用谷歌設計支持庫25.0.0 在我的活動相關的我有BottomSheetBehaviour:視圖不與BottomSheetBehavior

app:layout_behavior="android.support.design.widget.BottomSheetBehavior" 

現在相對佈局時,我引用它添加BottomSheetBehaviour,我得到錯誤

java.lang.IllegalArgumentException: The view is not associated with BottomSheetBehavior 

這裏的佈局:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:map="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:id="@+id/maps_colayout" 
xmlns:app="http://schemas.android.com/tools" 
android:fitsSystemWindows="true" 
android:background="@color/white"> 

... 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="280dp" 
    android:layout_gravity="bottom" 
    android:id="@+id/rl_bottomsheet" 
    android:background="#F3F3F3" 
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior"> 

    ... 

</RelativeLayout> 

這裏是活動的相關代碼:

CoordinatorLayout colayout = (CoordinatorLayout) findViewById(R.id.maps_colayout); 
    View bottomSheet = colayout.findViewById(R.id.rl_bottomsheet); 
    BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet); 
    behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() { 
     @Override 
     public void onStateChanged(@NonNull View bottomSheet, int newState) { 
      // React to state change 
     } 

     @Override 
     public void onSlide(@NonNull View bottomSheet, float slideOffset) { 
      // React to dragging events 
     } 
    }); 

回答

3

你有錯app命名空間中聲明。與

xmlns:app="http://schemas.android.com/apk/res-auto" 
在佈局文件你CoordinatorLayout聲明

xmlns:app="http://schemas.android.com/tools" 

:更換線路。

tools命名空間用於向「工具」(例如IDE)提供有關佈局的附加信息。此信息從應用中刪除。

另一方面,app命名空間是所有自定義(即未由Android系統聲明)屬性的全局命名空間,或者由您或導入的庫聲明(設計支持庫就是您的情況)。這些都包含在您的應用中。

那麼實際上tools命名空間有什麼好處?最常見的用法是更好地呈現佈局的預覽。舉個例子,假設你有一個TextView,它應該最初是空的並在稍後填充。

您可以將屬性添加到您的TextView聲明:

tools:text="Some example text here" 

顯示在您的應用程序本文不走了。但是,您的Android Studio中呈現的佈局預覽版會預覽它,因此您可以看到移動設備上的預期效果。

+0

工作。我怎麼知道何時使用/工具或/ apk/res-auto –

+0

我更新了我的答案,現在清楚了嗎? –