Android自定义ViewGroup横向布局(1)
更新时间:2016年12月20日 13:38:59 作者:封魔之殇
这篇文章主要介绍了Android自定义ViewGroup横向布局,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
最近学习自定义viewgroup,我的目标是做一个可以很想滚动的listview,使用adapter填充数据,并且使用adapter.notifyDataSetChanged()更新数据。
不过一口吃不成一个胖子(我吃成这样可是好几年的积累下来的~~~~),我们一步一步来,这篇笔记首先写一个横向的布局。
代码:
package com.example.libingyuan.horizontallistview.ScrollViewGroup;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
/**
* 自定义ViewGroup
* 很简单的横向布局,把所有的子View都横着排列起来,不可滚动
*/
public class ScrollViewGroup extends ViewGroup{
public ScrollViewGroup(Context context) {
this(context,null);
}
public ScrollViewGroup(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public ScrollViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//重新设置宽高
this.setMeasuredDimension(measureWidth(widthMeasureSpec,heightMeasureSpec),measureHeight(widthMeasureSpec,heightMeasureSpec));
}
/**
* 测量宽度
*/
private int measureWidth(int widthMeasureSpec, int heightMeasureSpec) {
// 宽度
int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
//宽度的类型
int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
//父控件的宽(wrap_content)
int width = 0;
//子View的个数
int childCount = getChildCount();
//重新测量子view的宽度,以及最大高度
for (int i = 0; i < childCount; i++) {
//获取子View
View child = getChildAt(i);
//测量子View,无论什么模式,这句必须有否则界面不显示子View(一片空白)
measureChild(child, widthMeasureSpec, heightMeasureSpec);
//得到子View的边距
MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
//得到宽度
int childWidth = child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
//宽度累加
width += childWidth;
}
//返回宽度
return modeWidth == MeasureSpec.EXACTLY ? sizeWidth : width;
}
/**
* 测量高度
*/
private int measureHeight(int widthMeasureSpec, int heightMeasureSpec) {
//高度
int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);
//高度的模式
int modeHeight = MeasureSpec.getMode(heightMeasureSpec);
//父控件的高(wrap_content)
int height = 0;
//子View的个数
int childCount = getChildCount();
//重新测量子view的宽度,以及最大高度
for (int i = 0; i < childCount; i++) {
//得到子View
View child = getChildAt(i);
//测量
measureChild(child, widthMeasureSpec, heightMeasureSpec);
//得到边距
MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
//得到高度
int childHeight = child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;
//累加高度
height += childHeight;
}
//求平均高度
height = height / childCount;
//返回高度
return modeHeight == MeasureSpec.EXACTLY ? sizeHeight : height;
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int childLeft=0;//子View左边的距离
int childWidth;//子View的宽度
int height=getHeight();
int childCount=getChildCount();
for (int i = 0; i < childCount; i++) {
View child=getChildAt(i);
MarginLayoutParams lp= (MarginLayoutParams) child.getLayoutParams();
childWidth=child.getMeasuredWidth()+lp.leftMargin+lp.rightMargin;
//最主要的一句话
child.layout(childLeft,0,childLeft+childWidth,height);
childLeft+=childWidth;
}
}
@Override
public LayoutParams generateLayoutParams(AttributeSet attrs) {
return new MarginLayoutParams(getContext(),attrs);
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
- Android动画效果之自定义ViewGroup添加布局动画(五)
- Android自定义ViewGroup之实现FlowLayout流式布局
- Android编程重写ViewGroup实现卡片布局的方法
- Android App开发中自定义View和ViewGroup的实例教程
- Android自定义ViewGroup的实现方法
- Android自定义ViewGroup打造各种风格的SlidingMenu
- 从源码解析Android中View的容器ViewGroup
- Android自定义ViewGroup实现标签浮动效果
- Android应用开发中自定义ViewGroup的究极攻略
- Android自定义ViewGroup实现带箭头的圆角矩形菜单
相关文章
Android多国语言转换Excel及Excel转换为string详解
这篇文章主要给大家介绍了关于Android多国语言转换Excel以及Excel转换为string的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起看看吧2019-01-01
Android自定义View实现通讯录字母索引(仿微信通讯录)
本文主要介绍了Android自定义View实现通讯录字母索引(仿微信通讯录)的实现步骤与方法,具有很好的参考价值,下面跟着小编一起来看下吧2016-12-12
android studio无法添加 bmob sdk依赖问题及解决方法
这篇文章主要介绍了android studio无法添加 bmob sdk依赖,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2020-05-05
Android SDK Manager国内无法更新的解决方案
本文主要介绍Android SDK Manager国内无法更新的解决方案,这里提供了解决方法,及简单说明实现流程,有兴趣的小伙伴可以参考下2016-09-09


最新评论