gpt4 book ai didi

java - 如何制作带有记事本风格背景的ListView?

转载 作者:行者123 更新时间:2023-12-02 00:11:30 24 4
gpt4 key购买 nike

我希望我的 ListView 看起来像记事本,即具有水平线背景图案。按照记事本示例,我可以扩展 TextView 并重写其 onDraw(),如下所示:

r = new Rect();
for (int i = 0; i < getLineCount(); i++) {
int baseline = getLineBounds(i, r);
canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1, paint);
}
super.onDraw(canvas);

但是当列表中只有几个元素时,将没有足够的行来填充页面(实际结果在左侧,所需结果在右侧):

enter image description here ~~~enter image description here

所以我尝试了另一种方法:重写ListView.onDraw()。不幸的是,没有直接的方法来计算顶部滚动(getScrollY()总是返回0),最重要的是,我必须禁用所有缓存和绘图优化,除了无法针对大型列表进行扩展之外,这肯定会降低性能。

最后,我的行小部件不是纯 TextView 。即使主要内容是 TextView,它们也是复杂的布局。这意味着在布局内部我无法调用 getLineBounds() (布局不是 TextView ),在 TextView 中我也不能调用 getLineBounds() ,因为 TextView比周围的布局小,所以四个边都会有间隙。

如何构建一个解决方案来显示我的自定义小部件并用水平线填充整个窗口?一种幼稚的方法是将虚拟空元素添加到列表中,只要它适合所有可用空间即可,但是这是一种黑客行为,必须有更好的方法来执行操作。使用背景图像不是一种选择,因为线之间的距离必须在运行时可自定义。

最佳答案

下面的代码基于您问题中的简单示例,一个自定义的 TextView 在底部绘制一条线(并且列表中没有分隔符)。在这种情况下,我将创建一个自定义 ListView 并重写 dispatchDraw 方法,如下所示:

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.RED);
mPaintBackground.setColor(Color.CYAN);
}

@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)
return;
} else {
// get the last drawn child
final View lastChild = getChildAt(getChildCount() - 1);
// 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);
for (int i = 0; i < nrOfLines; i++) {
canvas.drawLine(0, lastChildBottom + (i + 1) * lastChildHeight,
getMeasuredWidth(), lastChildBottom + (i + 1)
* lastChildHeight, mPaint);
}
}
}

}

看看您是否可以使用上面的代码并根据您自己的需要进行调整。

关于java - 如何制作带有记事本风格背景的ListView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12737600/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com