2016-09-05 31 views
0

我在滚动视图中有一个进度条,我想以编程方式修改宽度,但它看起来像不可能,在xml中我只有这样的进度条:以编程方式更新水平进度条宽度(在水平滚动视图中)

<HorizontalScrollView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 

    <ProgressBar 
     android:id="@+id/progressbar" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     style="?android:attr/progressBarStyleHorizontal" /> 
</HorizontalScrollView> 

和代码也很简单:

ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressbar); 
progressBar.setMinimumWidth(3000); 
progressBar.invalidate(); // tried called invalidate although setMinimumWidth called requestLayout, not help 

我也试图设置布局宽度,像这样:

progressBar.getLayoutParams().width = 3000; 
progressBar.invalidate(); 

但发现它只适用于不在滚动视图中的进度条...

任何想法如何更新进度条宽度(在水平滚动视图中)?以及为什么滚动视图缺陷进度条宽度? 谢谢!

+1

将来,请不要删除问题,然后重新提出问题。 – CommonsWare

+0

@CommonsWare,我这样做是因为原来的问题发生了很大的变化,但得到了你的评论,并且将来不会这样做。谢谢! – AsfK

+0

“我这样做是因为原始问题发生了很大的变化” - 这就是“编辑”链接的作用。 :-) – CommonsWare

回答

3

你没有设置params正确的方式。试试这个:

HorizontalScrollView.LayoutParams params = (HorizontalScrollView.LayoutParams) progressBar.getLayoutParams(); 
params.width = 3000; 
progressBar.setLayoutParams(params);