简体   繁体   English

是否可以在Xamarin Studio(Android)中使ProgressBar垂直放置?

[英]Is it possible to make ProgressBar vertical in Xamarin Studio (Android)?

I need to place a small vertical progress bar in each cell of a ListView. 我需要在ListView的每个单元格中放置一个小的垂直进度栏。 I have spent lots of hours trying to make my own custom progress bar using different techniques. 我花了很多时间尝试使用不同的技术制作自己的自定义进度栏。 Here are some of them: I tried placing two ImageViews one above the other, switching their background color to look similar to a standard ProgressBar. 以下是其中一些:我尝试将两个ImageView放在另一个之上,将它们的背景色切换为与标准ProgressBar相似。 And the thing was to switch their position and height to make it look like the progress is proceeding. 事情是改变他们的位置和身高,使其看起来像进步一样。 Another solution was to use a custom shape 另一个解决方案是使用自定义形状

But I always faced the same problem: the views wouldnot redraw until I do any useraction. 但是我总是面临着同样的问题:在我执行任何用户操作之前,视图不会重绘。 Anyways, I was thinking, what if I use a standard ProgressBar, which does redraw automatically, but rotate it. 无论如何,我一直在想,如果我使用标准的ProgressBar,它会自动重绘但旋转它会怎样?

Does anyone know how to rotate a ProgressBar or any other solution to my issue? 有谁知道如何旋转ProgressBar或其他解决我问题的方法?

Finally answering my own question. 最后回答我自己的问题。 In order to make a standard progress bar vertical you should create a class which inherits from ProgressBar and override the Draw method: 为了使标准进度条垂直,您应该创建一个从ProgressBar继承并覆盖Draw方法的类:

public class VerticalProgressBar : ProgressBar
{
    protected VerticalProgressBar(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
    {
    }

    public VerticalProgressBar(Context context) : base(context)
    {
    }

    public VerticalProgressBar(Context context, IAttributeSet attrs) : base(context, attrs)
    {
    }

    public VerticalProgressBar(Context context, IAttributeSet attrs, int defStyle) : base(context, attrs, defStyle)
    {
    }

    protected override void OnSizeChanged (int w, int h, int oldw, int oldh)
{
    base.OnSizeChanged (h, w, oldh, oldw); //Fixes the width-height issue
}

public override void Draw(Android.Graphics.Canvas canvas)
{
    canvas.Rotate(-90); //Rotating the canvas around (0,0) point of the control
    canvas.Translate(-Height, 0); //Moving the canvas inside the control rect
    base.OnDraw(canvas);
}
}

After that you can either add the control in code, or use the new control in your .axml document writing the namespace containing it in front like this: 之后,您可以在代码中添加控件,也可以在.axml文档中使用新控件,像这样在前面编写包含它的名称空间:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/widget"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <YOUR_NAMESPACE.VerticalProgressBar
        android:id="@+id/progress"
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:layout_width="10px"
        android:layout_height="100px" />
    <LinearLayout
        android:id="@+id/linearText"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:layout_marginLeft="10px"
        android:layout_marginTop="10px">
        <TextView
            android:id="@+id/textTop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />
        <TextView
            android:id="@+id/textBottom"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />
    </LinearLayout>
</RelativeLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM