歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android開發教程:自定義ViewGroup方法總結

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

日期:2017/3/1 10:02:58   编辑:Linux編程

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

<?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());
}
}

Copyright © Linux教程網 All Rights Reserved