2014-04-02 243 views
0

我使用ProgressBar作为自定义ListView的一部分。现在,我想根据屏幕显示ProgressBar的宽度,其值为ProgressBar

对于如果进度值是100,那么它应该显示其宽度为设备屏幕的100%,并且对于所有0-100值都应该如此。

我的代码如下:

layout.xml:根据进度值设置进度条的宽度并根据设备屏幕

<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_marginLeft="5dp" 
    android:layout_marginRight="5dp" 
    android:layout_marginTop="3dp" > 

    <ProgressBar 
     android:id="@+id/pbTopicAccuracy" 
     style="?android:attr/progressBarStyleHorizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="20dp" /> 

    <TextView 
     android:id="@+id/tvAccuracy" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@id/pbTopicAccuracy" 
     android:layout_alignLeft="@id/pbTopicAccuracy" 
     android:layout_alignTop="@id/pbTopicAccuracy" 
     android:paddingLeft="5dp" 
     android:textColor="@color/black" 
     android:textSize="12sp" /> 
</RelativeLayout> 

Java代码:

adapter.setViewBinder(new SimpleAdapter.ViewBinder() { 

    @Override 
    public boolean setViewValue(View view, Object data, String textRepresentation) { 

     if (view.getId() == R.id.pbTopicAccuracy) { 
      int value = Integer.parseInt(data.toString()); 
      ((ProgressBar) view).setProgress(value); 

      view.setBackgroundResource(R.drawable.report); 

      RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(value, 30); 
      ((ProgressBar) view).setLayoutParams(params); 
     } 
    } 
}); 

它工作正常,但它不设置宽度按照它应该是。如果值为100,则它将宽度设置为像设备屏幕的4.5部分。

如何防止这个问题,并获得适当的宽度?

回答

0

是不是您定义的导致问题的边距?

android:layout_marginLeft="5dp" 
android:layout_marginRight="5dp" 

如果不是,你可以用照片解释你的问题吗?

+0

不,这是不同的东西。因为这个RelativeLayout是LinearLayout的父类。 –

+0

你可以做你的问题的截图吗? –

0

我已经通过下面的代码解决了这个问题:

Display display = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); 
    int ratio = ((int) display.getWidth() /100) * value ; 
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ratio, 30); 
      ((ProgressBar) view).setLayoutParams(params); 
相关问题