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
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());
}
}
相關文章
- Android自定義ViewGroup(一)AndroidView
- android自定義View&自定義ViewGroup(下)AndroidView
- android自定義View&自定義ViewGroup(上)AndroidView
- Android自定義View:ViewGroup(三)AndroidView
- Android ViewDragHelper 自定義 ViewGroup 神器AndroidView
- Android ViewDragHelper完全解析 自定義ViewGroup神器AndroidView
- 快速開發偷懶必備,搞定所有ViewGroup的Adapter . 支援自定義ViewGroupViewAPT
- Android自定義控制元件之自定義ViewGroup實現標籤雲Android控制元件View
- Android動畫效果之自定義ViewGroup新增布局動畫Android動畫View
- Android自定義View之事件分發機制總結AndroidView事件
- ViewGroup篇:玩一下自定義ViewGroupView
- android 實現FlowLayout 流線佈局(自定義ViewGroup)AndroidView
- 自定義ViewGroup,實現Android的側滑選單ViewAndroid
- Android自定義ViewGroup View的大小和座標控制AndroidView
- android view 自定義viewgroup 例項--螢幕滑動AndroidView
- Android開發之自定義SpinnerAndroid
- 自定義view總結View
- 自定義View以及事件分發總結View事件
- Android開發教程 - 使用Data Binding(八)使用自定義InterfaceAndroid
- Android 中自定義 View、ViewGroup 理論基礎詳解AndroidView
- Android中自定義View、ViewGroup理論基礎詳解AndroidView
- 一篇文章搞懂Android 自定義viewgroup的難點AndroidView
- Android開發之自定義View(一)AndroidView
- Android開發之自定義View(二)AndroidView
- 安卓開發之ViewDragHelper的使用及自定義可下拉展示內容的ViewGroup安卓View
- 二、自定義垂直ViewGroup如何設定marginView
- flutter自定義View(CustomPainter) 之 canvas的方法總結FlutterViewAICanvas
- Android Studio NDK開發:自定義庫Android
- Flutter Widget自定義總結Flutter
- iOS自定義 Transitions 動畫總結iOS動畫
- Android開發自定義View之滑動按鈕與自定義屬性AndroidView
- 【Android開發點滴】自定義Toast樣式AndroidAST
- 自定義viewgroup(6)--使用adapter適配資料ViewAPT
- Android開發經驗總結Android
- Android TV開發總結【RecycleView】AndroidView
- Android 開發功能模組總結Android
- android短視訊開發,自定義下拉選單Android
- Android技術分享| 自定義ViewGroup實現直播間大小屏無縫切換AndroidView