2013-05-06 39 views
0

我需要實現一個下拉視圖,在ActionBar的最右側有一個「句柄」。它應該是全寬度,並且在點擊該句柄時用動畫打開,並且另外該句柄本身應該是可拖動的。 minSdkVersion is 8從ActionBar下拉視圖(菜單)

關於下拉功能本身,我發現SlidingDrawer不適合賬單,因爲它已被棄用到API v17,它只能從下到上打開。控制SlidingTray似乎解決了這個問題。我沒有徹底測試它,但它似乎按預期工作。

現在的主要問題。是否有可能以這種方式顯示視圖?我曾嘗試以設置動作條,其中充氣XML看起來像這樣的自定義視圖:現在

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

    <my.package.drawer.SlidingTray 
     android:id="@+id/drawer" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_alignParentRight="true" 
     android:content="@+id/content" 
     android:handle="@+id/handle" > 

     <ImageView 
      android:id="@+id/handle" 
      android:layout_width="88dp" 
      android:layout_height="44dp" 
      android:src="@drawable/ic_launcher" /> 

     <Button 
      android:id="@+id/content" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" /> 
    </my.package.drawer.SlidingTray> 
</RelativeLayout> 

,該SlidingTray觀點本身是否正常,當我把它的活動/片段佈局如預期(可以拖動手柄並單擊它可以打開/關閉托盤),但是在對ActionBar內部的上方佈局進行充氣時,按下/拖動手柄後,托盤只會在停止前移動幾個像素 - 它不會超出ActionBar界限。這是主要問題 - 視圖是否超出了ActionBar本身(通過下面顯示的活動),如果是這樣 - 如何?

回答

1

由於沒有人回答,我會發布我如何解決這個問題。 經過對hierarchyviewer的一些檢查之後,我看到ActionBar在一個LinearLayout中,因此,它不可能在ActionBar邊界之外擴展它的一個子節點。所以我決定獲取根(裝飾)視圖並在那裏附上SlidingDrawer的修改版本。下面是摘錄:

ViewGroup decor = (ViewGroup) getWindow().getDecorView(); 
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
View drawerContainer = inflater.inflate(R.layout.sliding_drawer, null); 
drawer = (SlidingDrawer) drawerContainer.findViewById(R.id.drawer); 
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); 
decor.addView(drawerContainer, params); 

由於添加視圖這種方式將其顯示在狀態欄後面,我還使得手柄和內容被顯示在其下方加有25dp的頂部填充的容器圖。

注意:如果您使用的是SlidingMenu庫,則需要在onPostCreate()中執行此操作,因爲庫也會這樣做,並會將您的視圖放在所有其他內容的後面。