首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Android listview中显示空行

在Android listview中显示空行
EN

Stack Overflow用户
提问于 2012-02-20 22:54:53
回答 5查看 3K关注 0票数 4

所以我想知道如何用空行填充ListViewListView是通过SQLite db填充的,例如,假设列表中只有3个项目,我希望用空行填充屏幕的其余部分。这是我想说的截图。是的,我知道它来自iPhone,但它说明了我的意思:

EN

回答 5

Stack Overflow用户

发布于 2014-02-06 03:44:26

其他人已经避开了它,但我认为发布一个代码片段可能会有所帮助。如果有更好的技术,如果我学到了什么,我会很乐意投反对票的。

最便宜的方法是在布局xml中添加额外的“行”。任何多余的行都会被屏幕截断。这可能会变得混乱:更高分辨率的屏幕可能需要您添加许多额外的TextViews。既然它们被切断了,我想你可以想添加多少就添加多少。它看起来像这样:

代码语言:javascript
运行
复制
<LinearLayout>

<ListView></ListView>

<TextView/>
<TextView/>
<TextView/>
...

</LinearLayout>

另一种选择是向ListView中添加额外的行,如前所述。如果绑定到数组,请向数组中添加额外的空行并进行适当处理。巧合的是,如果您正在使用游标,则可以使用MaxtrixCursor & MergeCursor,如本文所述:https://stackoverflow.com/a/16440093/103131

我最终使用了这些方法的组合。我尽最大努力计算要添加到ListView中的行数,但出于谨慎考虑,我出错了,在我的ListView下面有两个TextViews,这使得所有的行都在一起。

票数 3
EN

Stack Overflow用户

发布于 2012-02-20 22:56:09

当您创建数组时,将绑定到ListView,您只需在数组的末尾添加几行空字符串。

票数 2
EN

Stack Overflow用户

发布于 2014-12-11 13:59:15

代码语言:javascript
运行
复制
just  make  sure     android:layout_height="match_parent"

<RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
            <your.app.package.CustomListView
                android:id="@+id/editableTourListView"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </your.app.package.CustomListView>
        </RelativeLayout> 





    public class CustomListView extends ListView {
        private Paint mPaint = new Paint();
        private Paint mPaintBackground = new Paint();
        public CustomListView(Context context, AttributeSet attrs) {
            super(context, attrs);
            mPaint.setColor(Color.BLACK);
            mPaintBackground.setColor(Color.WHITE);
        }       
        @Override
        protected void dispatchDraw(Canvas canvas) {
            super.dispatchDraw(canvas);
            // ListView's height
            final int currentHeight = getMeasuredHeight();
            // this will let you know the status for the ListView, fitting/not
            // fitting content
            final int scrolledHeight = computeVerticalScrollRange();
            //        if (scrolledHeight >= currentHeight || scrolledHeight == 0) {
            // there is no need to draw something(for simplicity I assumed that
            // if the adapter has no items i wouldn't draw something on the
            // screen. If you still do want the lines then pick a decent value
            // to simulate a row's height and draw them until you hit the
            // ListView's getMeasuredHeight)
            //        } else {
            final View lastChild = getChildAt(getChildCount() - 1);
            if(lastChild==null) return;
            // values used to know where to start drawing lines
            final int lastChildBottom = lastChild.getBottom();
            // last child's height(use this to determine an appropriate value
            // for the row height)
            final int lastChildHeight = lastChild.getMeasuredHeight();
            // determine the number of lines required to fill the ListView
            final int nrOfLines = (currentHeight - lastChildBottom)
                    / lastChildHeight;
            // I used this to simulate a special color for the ListView's row background
            Rect r = new Rect(0, lastChildBottom, getMeasuredWidth(),
                    getMeasuredHeight());           
            canvas.drawRect(r, mPaintBackground);
            canvas.drawLine(0, lastChildBottom ,
                    getMeasuredWidth(), lastChildBottom, mPaint);
            for (int i = 0; i < nrOfLines; i++) {
                canvas.drawLine(0, lastChildBottom + (i + 1) * lastChildHeight,
                        getMeasuredWidth(), lastChildBottom + (i + 1)
                        * lastChildHeight, mPaint); 
            }
            return;
            //        }
    }
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9363119

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档