Viewgroup(View容器)
来源:互联网 发布:windows ant下载 编辑:程序博客网 时间:2024/06/05 22:55
容器内的view
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.example.fanjie.mockone.TitleView android:id="@+id/ttv_title" android:layout_width="match_parent" android:layout_height="35dp"> </com.example.fanjie.mockone.TitleView> <com.example.fanjie.mockone.MyViewGroup android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="100dp" android:background="#0000ff" android:layout_height="50dp" /> <TextView android:layout_width="100dp" android:background="#17b293" android:layout_height="50dp" /> <TextView android:layout_width="100dp" android:background="#b2eb09" android:layout_height="50dp" /> </com.example.fanjie.mockone.MyViewGroup></LinearLayout>创建MyViewGroup
import android.content.Context;import android.graphics.Canvas;import android.util.AttributeSet;import android.view.View;import android.view.ViewGroup;/** * author:Created by WangZhiQiang on 2017/11/3. */public class MyViewGroup extends ViewGroup { public MyViewGroup(Context context) { super(context); } public MyViewGroup(Context context, AttributeSet attrs) { super(context, attrs); } public MyViewGroup(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } //一般情况下不会再ViewGroup使用onDraw()方法,ViewGroup主要承担的是盛放View的职责 @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas);// Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);// paint.setColor(Color.RED);// canvas.drawCircle(50,50,50,paint); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); //测量View的宽高,只有ViewGroup中有这个方法,直接继承自View是没有这个方法的 measureChildren(widthMeasureSpec, heightMeasureSpec);// //ViewGroup自己的大小// setMeasuredDimension(DensityUtil.dip2px(getContext(),200),DensityUtil.dip2px(getContext(),200)); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { //拿到子控件的个数 int childCount = getChildCount(); //定义一个临时的高度变量 int startHeight=0; int startWidth=0; //循环遍历每一个View for(int i=0;i<childCount;i++){ View v = getChildAt(i); //给每一个View设置自己的位置 //v.getMeasuredHeight()自身的高度, //v.getMeasuredWidth()自身宽度 v.layout(startWidth,startHeight,startWidth+v.getMeasuredWidth(),startHeight+v.getMeasuredHeight()); startHeight+=v.getMeasuredHeight(); startWidth+=v.getMeasuredWidth(); } }}
阅读全文
0 0
- Viewgroup(View容器)
- 自定义view,继承ViewGroup实现可换行的布局容器
- view viewGroup
- ViewGroup---View
- Android自定义View--用ViewGroup打造一个3D堆叠卡片容器
- Android View、ViewGroup
- Android 之 View ViewGroup
- View和ViewGroup
- android view viewGroup 解析
- ViewGroup View触摸事件
- View和ViewGroup
- View和 ViewGroup
- android自定义View(viewGroup)
- View和ViewGroup
- android view 以及 viewGroup
- 自定义View及ViewGroup
- Android View.ViewGroup.LayoutParams
- 关于view和viewgroup
- LA3027
- 5、位运算符(7个):实例
- 软设之路-4.1(数据结构及算法)
- 2017 Fall SoftwareEngineering Learning (6)
- LeetCode 476 Number Complement 补数
- Viewgroup(View容器)
- Pat 1016. 部分A+B (15)
- Android开发-认识palette
- SQL SERVER 2008 R2 增删改查之01
- 最小费用最大流
- 用于代码生成的抽象语法网络 (侵图删)
- 115
- [dubbo]浅谈dubbo-常用标签
- servlet组件技术