ViewGroup重写——网格容器
来源:互联网 发布:js自定义属性 编辑:程序博客网 时间:2024/06/12 23:07
本文介绍ViewGroup重写,我们所熟知的LinearLayout,RelativeLayout,FrameLayout等等,所有的容器类都是ViewGroup的子类,ViewGroup又继承View。我们在熟练应用这些现成的系统布局的时候可能有时候就不能满足我们自己的需求了,这是我们就要自己重写一个容器来实现效果。
ViewGroup重写可以达到各种效果,下面写一个简单的重写一个ViewGroup来感受一下。
我们来写一个简单的网格容器,网格可以分行列,可以控制行列数,增加子控件的时候自动计算位置,如图:
1.继承ViewGroup类,重写onLayout,onLayout可以控制子View的显示位置。
2.实现OnHierarchyChangeListener接口,重写onChildViewAdded、onChildViewRemoved方法,这两个方法分别写在ViewGroup中添加和移除子View的时候会调用。
public class MyViewPager extends ViewGroup implements OnHierarchyChangeListener {TextView textView;int mCellCount = 4;public MyViewPager(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr); Log.i("Gmw", "MyViewPager3:");}public MyViewPager(Context context, AttributeSet attrs) {super(context, attrs); Log.i("Gmw", "MyViewPager2:"); setOnHierarchyChangeListener(this);//监听}public MyViewPager(Context context) {super(context); Log.i("Gmw", "MyViewPager1:");}@Overridepublic void onChildViewAdded(View parent, View child) {//添加的时候会调用Log.i("Gmw", "onChildViewAdded:"); }@Overridepublic void onChildViewRemoved(View parent, View child) {//移除的时候会调用Log.i("Gmw", "onChildViewRemoved:");}@Overrideprotected void onLayout(boolean changed, int l, int t, int r, int b) {//遍历所有的子控件,给每一个子控件设置位置for (int i = 0; i < getChildCount(); i++) {int x = (i % mCellCount) * 200;int y = (i / mCellCount) * 200;getChildAt(i).layout(x, y, x + 200, y + 200);// Log.i("Gmw", "onLayout:count=" + getChildCount() + ",index=" + i// + "," + x + "," + y + ",");}}}
使用:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/add_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Add" /> <Button android:id="@+id/remove_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Remove" /> </LinearLayout> <com.example.onlayouttext.MyViewPager android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/test" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="AAAAAA" /> </com.example.onlayouttext.MyViewPager></LinearLayout>Java:
public class MainActivity extends Activity {MyViewPager myViewPager;Button mBtnAdd, mBtnRemove;int i = 0;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.view);myViewPager = (MyViewPager) findViewById(R.id.viewpager);mBtnAdd = (Button) findViewById(R.id.add_btn);mBtnRemove = (Button) findViewById(R.id.remove_btn);mBtnAdd.setOnClickListener(new OnClickListener() {public void onClick(View v) {TextView textView = new TextView(getBaseContext());textView.setText("COUNT=" + myViewPager.getChildCount());myViewPager.addView(textView);}});mBtnRemove.setOnClickListener(new OnClickListener() {public void onClick(View v) {if ((myViewPager.getChildCount() - 1) >= 0) {myViewPager.removeViewAt(myViewPager.getChildCount() - 1);Log.i("Gmw", "removeViewAt=" + (myViewPager.getChildCount() - 1));}}});}}
看控件效果:
0 0
- ViewGroup重写——网格容器
- ViewGroup重写——滚动页面容器
- Android进阶——自定义View之重写ViewGroup组合系统控件实现自定义ToolBar模板
- 重写ViewGroup注意事项
- Viewgroup(View容器)
- GridLayout—网格布局
- Android仿人人客户端(v5.7.1)——采用ViewGroup做父容器,实现左侧滑动菜单(三)
- 自定义ViewGroup——初识
- 重写ViewGroup实现App第一次启动向导
- 继承ViewGroup重写onMeasure方法的详解
- 继承ViewGroup需要重写的方法
- 重写viewgroup实现左右滑动(修改)
- 继承ViewGroup重写onMeasure方法的详解
- Android RippleEffect波纹效果,重写ViewGroup
- viewgroup重写哦你measure方法
- 自定义ViewGroup需要重写的方法
- (转)Android RippleEffect波纹效果,重写ViewGroup
- Bootstrap——网格系统
- 香港--身份证(本国人)正则表达式
- 大陆--身份证(外国人)正则表达式
- 黑马程序员——java-集合框架(二)
- UVA 12170 Easy Climb(dp+单调队列优化)
- 第一篇文章:单例模式的6种写法
- ViewGroup重写——网格容器
- An R "meta" book
- 整数因子分解算法
- Html.partial和RenderPartial的用法与区别
- 台湾--身份证(外国人)正则表达式
- 澳门--身份证(外国人)正则表达式
- 懂二进制
- 计数排序算法
- 香港--身份证(外国人)正则表达式