我在水平滚动视图中有一个线性布局,我想根据其内容调整填充。我正在使用水平滚动视图的onLayout调用。
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
if(null != views && views.size() > 0) {
int paddingLeft = (getWidth()/2) - (views.get(0).getWidth()/2);
int paddingRight = (getWidth()/2) - (views.get(views.size()-1).getWidth()/2);
content.setPadding(getPaddingLeft() + paddingLeft, getPaddingTop(), getPaddingRight() + paddingRight, getPaddingBottom());
}
content.invalidate();
}
我记录了所有尺寸,除了填充外,所有尺寸似乎都很准确。在我再次更改视图的可见性之前,内容上的填充不可见。正如您所看到的,我尝试使内容无效,但在重新绘制之前,布局的宽度仍然没有填充。是否可以强制抽奖?
答案 0 :(得分:0)
我通过获取onLayout中的填充值并在绘制之前设置它来解决它,希望它可以帮助其他人。
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
FLog.d("Information", "onLayout");
if(null != views && views.size() > 0) {
paddingLeft = (getWidth()/2) - (views.get(0).getWidth()/2);
paddingRight = (getWidth()/2) - (views.get(views.size()-1).getWidth()/2);
FLog.i("Information", "[paddingRight]" + paddingRight + " = " + " ([scrollView width]" + getWidth() + " / 2) " + " - " + "[viewLast width]" + views.get(views.size()-1).getWidth() + " / 2)");
FLog.i("Information", "ContentWidth: " + content.getWidth());
paddingInitialized = true;
}
content.invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
if(!paddingSet && paddingInitialized) {
content.setPadding(getPaddingLeft() + paddingLeft, getPaddingTop(), getPaddingRight() + paddingRight, getPaddingBottom());
paddingSet = true;
}
super.onDraw(canvas);
}