日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android学习:自定义ViewGroup方法总结

發布時間:2025/6/15 Android 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android学习:自定义ViewGroup方法总结 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

畢設應用中需要添加一個滑動按鈕,在網上看了幾個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

[java] view plaincopy
  • 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
    l
    Left position, relative to parent
    t
    Top position, relative to parent
    r
    Right position, relative to parent
    b
    Bottom position, relative to parent

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

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

    創建布局文件myalbumlistwithmuen,使用MyViewGroup作為控件:

    [html] view plaincopy
  • <?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作為其布局:

    [java] view plaincopy
  • 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方法

    [java] view plaincopy
  • @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());??
  • ????}??
  • }?
  • 總結

    以上是生活随笔為你收集整理的Android学习:自定义ViewGroup方法总结的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。