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

发布于 2014-02-06 03:44:26
其他人已经避开了它,但我认为发布一个代码片段可能会有所帮助。如果有更好的技术,如果我学到了什么,我会很乐意投反对票的。
最便宜的方法是在布局xml中添加额外的“行”。任何多余的行都会被屏幕截断。这可能会变得混乱:更高分辨率的屏幕可能需要您添加许多额外的TextViews。既然它们被切断了,我想你可以想添加多少就添加多少。它看起来像这样:
<LinearLayout>
<ListView></ListView>
<TextView/>
<TextView/>
<TextView/>
...
</LinearLayout>另一种选择是向ListView中添加额外的行,如前所述。如果绑定到数组,请向数组中添加额外的空行并进行适当处理。巧合的是,如果您正在使用游标,则可以使用MaxtrixCursor & MergeCursor,如本文所述:https://stackoverflow.com/a/16440093/103131
我最终使用了这些方法的组合。我尽最大努力计算要添加到ListView中的行数,但出于谨慎考虑,我出错了,在我的ListView下面有两个TextViews,这使得所有的行都在一起。
发布于 2012-02-20 22:56:09
当您创建数组时,将绑定到ListView,您只需在数组的末尾添加几行空字符串。
发布于 2014-12-11 13:59:15
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;
// }
}
}https://stackoverflow.com/questions/9363119
复制相似问题