如何以编程方式设置Android ProgressBar的大小?

问题描述:

我有一个自定义视图,延伸ViewGroup。它包括一个ProgressBar和一个WebView。正在加载WebView时,我正在显示ProgressBar如何以编程方式设置Android ProgressBar的大小?

这可行,但ProgressBar太大。我如何让它变小?

下面是我的非工作代码:

webView = new WebView(context); 
webView.setWebViewClient(new MyWebChromeClient()); 
webView.loadUrl("file://" + path); 
addView(webView); 

progressBar = new ProgressBar(mContext, null, 
             android.R.attr.progressBarStyleSmall); 
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, 
                LayoutParams.WRAP_CONTENT); 
progressBar.setLayoutParams(params); 

addView(progressBar); 

可以使用的LayoutParams改变宽度和高度,以任何你想要的。

ProgressBar progressBar = new ProgressBar(teste.this, null, android.R.attr.progressBarStyleHorizontal); 

      LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(300, 10); 

      progressBar.setLayoutParams(params); 
      LinearLayout test = new LinearLayout(getApplicationContext()); 
      test.addView(progressBar); 
      setContentView(test); 

此外,当添加一个视图时,可以使用此代码: test.addView(progressBar,50,50);其中第一个参数是宽度和第二高度是。

您也可以从XML中设置你的进度条的大小,只需用ProgressBar标签内android:maxHeightandroid:minHeightandroid:minWidthandroid:maxWidth性能。

例如,这将进度条的高度设置为35dip和宽度10dip

<ProgressBar 
    android:id="@+id/progressBar1" 
    style="?android:attr/progressBarStyleLarge" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 

    android:layout_centerVertical="true" 
    android:layout_marginLeft="60dip" 
    android:maxHeight="35dip" 
    android:minHeight="35dip" 

    android:minWidth="10dip" 
    android:maxWidth="10dip" 

    android:visibility="visible" /> 
+3

这是行不通的 – Informatic0re 2015-09-29 13:37:55

的easies方法是规模:

public class CustomProgressBarRenderer :ProgressBarRenderer 
{ 
    protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.ProgressBar> e) 
    { 
    base.OnElementChanged(e); 

    Control.ProgressDrawable.SetColorFilter(Color.FromRgb(182, 231, 233).ToAndroid(), Android.Graphics.PorterDuff.Mode.SrcIn); 
    Control.ProgressTintList = Android.Content.Res.ColorStateList.ValueOf(Color.FromRgb(182, 231, 233).ToAndroid()); 
    Control.ScaleY = 10; // This changes the height 

    } 
} 

在XML中,设置样式,例如, style =“?android:attr/progressBarStyleLarge”:

<ProgressBar 
         android:id="@+id/progressBar3" 
         style="?android:attr/progressBarStyleLarge" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_centerInParent="true" /> 

让我们试试吧......

我已修复size的progressBar并添加一些padding

<RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 
     <ProgressBar 
      android:id="@+id/progressBar" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:padding="45dp" 
      /> 
     <ImageView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:id="@+id/category_img" 
      android:scaleType="fitXY" 
      tools:ignore="ContentDescription" /> 
    </RelativeLayout> 

现在看起来像这样:

enter image description here

+0

调整边距为我们提供了所需的尺寸进度:) – sunil 2017-09-08 09:55:47