如何省略显示过多的child views
2016-03-23 19:37
417 查看
实现过多子view的省略显示
多个icon在一个父容器内(LinearLayout),过多icon导致无法全部显示时加入省略号的实现方案:布局为大容器icon_area包含icon的父容器以及省略号的image view,将image view的引用给到icon对象。而自定义的icon的父容器IconMerger,负责计算child view是否超过最最大宽度,然后显示或隐藏省略号
布局xml如下:
<LinearLayout android:id="@+id/icon_area" android:layout_width="match_parent" android:layout_height="match_parent" > <IconMerger android:id="@+id/notificationIcons" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:layout_alignParentStart="true" android:gravity="center_vertical" android:orientation="horizontal" /> <ImageView android:id="@+id/moreIcon" android:layout_width="13dp" android:layout_height="match_parent" android:src="@drawable/stat_notify_more" android:visibility="gone" /> </LinearLayout>
自定义IconMerger实现:
public class IconMerger extends LinearLayout { private static final String TAG = "IconMerger"; private static final boolean DEBUG = false; private int mNotificationIconSize; private View mMoreView; public IconMerger(Context context, AttributeSet attrs) { super(context, attrs); mNotificationIconSize = context.getResources().getDimensionPixelSize( R.dimen.status_bar_notification_icon_size); } public void setOverflowIndicator(View v) { mMoreView = v; } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); // we need to constrain this to an integral multiple of our children int width = getMeasuredWidth(); //计算容器大小时,保证宽度恰好容纳整数个icon setMeasuredDimension(width - (width % mNotificationIconSize), getMeasuredHeight()); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { super.onLayout(changed, l, t, r, b); //onlayout时检测宽度是否超出 checkOverflow(r - l); } private void checkOverflow(int width) { if (mMoreView == null) return; final int N = getChildCount(); int visibleChildren = 0; for (int i=0; i<N; i++) { //记录所有非gone的icon数目 if (getChildAt(i).getVisibility() != GONE) visibleChildren++; } final boolean overflowShown = (mMoreView.getVisibility() == View.VISIBLE); // let's assume we have one more slot if the more icon is already showing if (overflowShown) visibleChildren --; final boolean moreRequired = visibleChildren * mNotificationIconSize > width; if (moreRequired != overflowShown) { post(new Runnable() { @Override public void run() { mMoreView.setVisibility(moreRequired ? View.VISIBLE : View.GONE); } }); } } }
相关文章推荐
- Struts2中 Result类型配置详解
- 为什么std::cin>>int input_value是不合法的
- 共享数据的包含const
- Common Lisp 笔记(1)
- jQuery学习笔记(一)
- linux中libevent-2.0.so.5 not found 解决 ldd 方法
- 第4周项目5(4) 用递归方法求解
- Delphi 中 FindWindow 和 FindWindowEx 找到外部进程,然后发送消息(比如最大化)
- 记录Hibernate的缓存知识
- Linux内核分析第五周学习总结:扒开系统调用的三层皮(下)
- Linux内核分析作业 NO.5
- 比特承诺的概念
- 第4周项目3:随机数函数应用于游戏-猜数字游戏
- fast-rcnn配置运行demo.py(Ubuntu14.04),不成功的朋友请与我(lee)联系,后面附带邮箱
- 张量的展开与matlab下的工具包操作
- Linux内核分析第五章读书笔记
- 关闭磁感应,光感,,
- App架构经验总结
- 学习Mybatis:Mybatis的使用
- Linux内核分析5:分析system_call中断处理过程