How to change height of progress bar in Xamarin Forms?

前端 未结 5 410
深忆病人
深忆病人 2021-01-05 11:05

How do I change the height of a Xamarin Forms ProgressBar in code? Am using Xamarin Forms V2.1.

.HeightRequest and .MinimumHeightRequ

相关标签:
5条回答
  • 2021-01-05 11:44
     <ProgressBar
      BackgroundColor="White"
      ProgressColor="#BCC7EF"
      Progress="0.7">
        <ProgressBar.ScaleY>
          <OnPlatform
            x:TypeArguments="x:Double"
            iOS="2"
            Android="1" />
          </ProgressBar.ScaleY>
    
    0 讨论(0)
  • 2021-01-05 11:44

    For me, the most simple solution is to use Xamarin.Forms.Visual.Material Then in your XAML, in progress bar set HeightRequest property to what you want and use Visual property as Material.

    0 讨论(0)
  • 2021-01-05 11:52

    @BillF is correct basically

    In the iOS renderer code try using

    this.Control.Transform = transform; 
    

    instead of

    this.Transform = transform;
    
    0 讨论(0)
  • 2021-01-05 12:00

    You need custom renderers for this:

    First create a class for your custom progress bar:

    public class CustomProgressBar :ProgressBar
    {
      public CustomProgressBar()
       {
       }
    }
    

    And then also add a new file for your custom progress bar renderer:

    For iOS:

    public class CustomProgressBarRenderer : ProgressBarRenderer
    {
        protected override void OnElementChanged(
         ElementChangedEventArgs<Xamarin.Forms.ProgressBar> e)
        {
            base.OnElementChanged(e);
    
            Control.ProgressTintColor = Color.FromRgb(182, 231, 233).ToUIColor();// This changes the color of the progress
        }
    
    
        public override void LayoutSubviews()
        {
            base.LayoutSubviews();
    
            var X = 1.0f;
            var Y = 10.0f; // This changes the height
    
            CGAffineTransform transform = CGAffineTransform.MakeScale(X, Y);
            Control.Transform = transform;
        }
    }
    

    [EDIT: fixed code above as per comment]

    For Android:

    public class CustomProgressBarRenderer :ProgressBarRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.ProgressBar> e)
        {
            base.OnElementChanged(e);
    
    
            Control.ProgressTintList = Android.Content.Res.ColorStateList.ValueOf(Color.FromRgb(182, 231, 233).ToAndroid()); //Change the color
            Control.ScaleY = 10; //Changes the height
    
        }
    }
    

    I hope this helps you!

    0 讨论(0)
  • 2021-01-05 12:04

    I faced the same need and on the latest version of Visual Studio, 16.5.2 I figured out that to get a bigger horizontal bar you just need to set ScaleY within the progressbar declaration inside the xml. To avoid glitches on Android and be sure that the progress bar is not overwhelming other elements I added a margin as you can see from the declaration here below.

            <ProgressBar
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/progressBar1"
            android:layout_below="@+id/message"
            android:scaleY="8"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"/>
    
    0 讨论(0)
提交回复
热议问题