前两天遇到一个需求,单行gridview要动态添加控件,而控件不足时布局是从左开始的,并不美观,网上并没有找到好方法,作为小白体验了一把自定义控件,因此拿出来给自己以及以后遇到的朋友提供一个思路吧。
<pre name="code" class="java">
<span style="white-space:pre"> </span>@Override
<span style="white-space:pre"></span>protected void onDraw(Canvas canvas) {
<span style="white-space:pre"></span>if (tempChildCount != getChildCount()) {
<span style="white-space:pre"></span>LOG.d("childCountChanged old=" + tempChildCount + " new=" + getChildCount());
<span style="white-space:pre"></span>tempChildCount = getChildCount();
<span style="white-space:pre"></span>DisplayMetrics dm = new DisplayMetrics();
<span style="white-space:pre">&l