作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我不确定什么是最好的标题,但是有图片,我想要的就很清楚了。
我已经实现了 Horizontal Scrollview,我已经自定义了它,因为只有四个项目会显示,如果用户想看到第五个项目,那么他必须滚动它。我已经成功地做到了。
但在 android 2.3.3 中,当有更多项目时它显示最后的白色 View ,而在 android 4.0 中它不显示。见下图:
在 android 2.3 中看这里,我显示的是白色 View 这清楚地告诉我,有更多的按钮,但在 android 4.0 或更高版本中我没有得到同样的结果。
任何人都可以帮助我如何显示它。
最佳答案
您可以通过扩展 HorizontalScrollView
小部件并绘制两个正确放置的图像/可绘制对象来轻松复制该行为:
public class CustomHorizontalScrollView extends HorizontalScrollView {
private static final int SHADOW_WIDTH = 35;
private GradientDrawable mDrawableLeft;
private GradientDrawable mDrawableRight;
private Rect mBounds = new Rect();
public CustomHorizontalScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
mDrawableLeft = new GradientDrawable(Orientation.LEFT_RIGHT,
new int[] { Color.GRAY, Color.TRANSPARENT });
mDrawableRight = new GradientDrawable(Orientation.RIGHT_LEFT,
new int[] { Color.GRAY, Color.TRANSPARENT });
}
@Override
protected void dispatchDraw(Canvas canvas) {
super.dispatchDraw(canvas);
// the scroll value
final int offset = this.getScrollX();
mBounds.setEmpty();
mBounds.bottom = getMeasuredHeight();
// check made to remove the shadow if we are at the left edge of the
// screen so we don't interfere with the edge effect
if (offset != 0) {
// left drawable
mBounds.left = offset;
mBounds.right = offset + SHADOW_WIDTH;
mDrawableLeft.setBounds(mBounds);
mDrawableLeft.draw(canvas);
}
// check made to remove the shadow if we are at the right edge of the
// screen so we don't interfere with the edge effect
if ((offset + getMeasuredWidth()) < computeHorizontalScrollRange()) {
// right drawable
mBounds.left = offset + getMeasuredWidth() - SHADOW_WIDTH;
mBounds.right = offset + getMeasuredWidth();
mDrawableRight.setBounds(mBounds);
mDrawableRight.draw(canvas);
}
}
}
关于android - 在 android 的 Horizontal ScrollView 中显示更多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20744037/
我是一名优秀的程序员,十分优秀!