2011-03-31 116 views
8

我有一個自定義視圖,延伸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); 

回答

19

可以使用的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);其中第一個參數是寬度和第二高度是。

12

您也可以從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

2

的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 

    } 
} 
2

在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" /> 
1

讓我們試試吧......

我已修復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

相關問題