android使用ViewGroup實現滑動菜單

Android開發教程:自定義ViewGroup方法總結

[日期:2013-01-18] 來源:Linux社區  作者:lissdy [字體:  ]

應用中需要添加一個滑動按鈕,在網上看了幾個Demo之後決定自定義ViewGroup來實現。

這裏是對實現過程中自定義ViewGroup的方法總結。

關於ViewGroup,文檔給出的描述是:

A ViewGroup is a special view that can contain other views (called children.) The view group is the base class for layouts and views containers.

ViewGroup是一種可以包含其他視圖的特殊視圖,是佈局和其他視圖容器的基類。

正因爲ViewGroup可以包含多個視圖,所以在實現滑動按鈕時可以使用它(一個主視圖,一個按鈕視圖)。

以自定義ViewGroup的名稱創建一個類,繼承ViewGroup

public class SlidingMenuView extends ViewGroup {
 public SlidingMenuView(Context context, AttributeSet attrs) {
  super(context, attrs);
  // TODO Auto-generated constructor stub
 }
 @Override
 protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {
  // TODO Auto-generated method stub  
 }
}

繼承ViewGroup之後,eclipse提示需要生成自定義ViewGroup的構造方法和onLayout方法。

onLayout方法是ViewGroup中的抽象方法,因此繼承ViewGroup之後一定要實現該方法。 
Called from layout when this view should assign a size and position to each of its children. Derived classes with children should override this method and call layout on each of their children.
Parameters:
changed 
This is a new size or position for this view

Left position, relative to parent

Top position, relative to parent

Right position, relative to parent

Bottom position, relative to parent


ViewGroup中的onLayout方法將在ViewGroup爲它的孩子們分配尺寸和位置的時候被調用,在這個類的實現中,需要調用每一個控件的佈局方法爲其佈局。

注意:onLayout在View中是一個public的方法,在ViewGroup爲protected類型,並且爲abstract,由於這個方法在ViewGroup中沒有實現,因此ViewGroup本身不可以直接使用。

創建佈局文件myalbumlistwithmuen,使用MyViewGroup作爲控件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >  
    <com.album.view.SlidingMenuView
        android:id="@+id/myviewgroup"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </com.album.view.SlidingMenuView>
</LinearLayout>

創建如圖兩個子視圖:主頁面myalbumlist、按鈕頁面slidingmenu:

 

創建Activity,使用myalbumlistwithmuen作爲其佈局:

public class MyAlbumListActivity extends Activity {
 private MyViewGroup myViewGroup;
 private LayoutInflater layoutInflater;
 private View slidingmenu, myalbumlist;
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.myalbumlistwithmuen);
  initView();
 } 
 private void initView(){
  myViewGroup=(MyViewGroup)findViewById(R.id.myviewgroup);
  layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  slidingmenu = layoutInflater.inflate(R.layout.slidingmenu, null);
  myalbumlist=layoutInflater.inflate(R.layout.myalbumlist, null);
  myViewGroup.addView(slidingmenu);  //添加滑動菜單的view
  myViewGroup.addView(myalbumlist); //添加主頁面的view
 }
}

上面代碼中,已經將滑動菜單視圖和主頁面視圖放入在自定義的ViewGroup當中。自定義的ViewGroup需要爲這兩個孩子分配尺寸和位置,故需重寫onLayout方法:

 @Override
 protected void onLayout(boolean changed, int l, int t, int r, int b) {
  if (changed) {
   slidingmenu = getChildAt(0);// 獲取滑動菜單的view
   myalbumlist = getChildAt(1);// 獲得主頁view
   // 相當於fill_parent
   myalbumlist.measure(0, 0);
   myalbumlist.layout(0, 0, getWidth(), getHeight());
  }
 }

linux
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章