你好,游客 登录 注册 搜索
背景:
阅读新闻

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

linux
相关资讯       Android开发教程  自定义ViewGroup 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款