【问题标题】:Is it possible to make ProgressBar vertical in Xamarin Studio (Android)?是否可以在 Xamarin Studio (Android) 中使 ProgressBar 垂直?
【发布时间】:2013-09-06 13:59:58
【问题描述】:

我需要在 ListView 的每个单元格中放置一个小的垂直进度条。 我花了很多时间尝试使用不同的技术制作自己的自定义进度条。这里是其中的一些: 我尝试将两个 ImageView 一个一个放在另一个之上,将它们的背景颜色切换为看起来类似于标准 ProgressBar。事情是改变他们的位置和高度,让它看起来像是在进行中。 另一种解决方案是使用custom shape

但我总是面临同样的问题:在我执行任何用户操作之前,视图不会重绘。 无论如何,我在想,如果我使用标准的 ProgressBar,它会自动重绘,但会旋转它。

有人知道如何旋转 ProgressBar 或任何其他解决我的问题的方法吗?

【问题讨论】:

    标签: c# android xamarin.android xamarin android-progressbar


    【解决方案1】:

    终于回答了我自己的问题。为了使标准进度条垂直,您应该创建一个继承自 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);
    }
    }
    

    之后,您可以在代码中添加控件,也可以在 .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>
    

    【讨论】:

    • 还有一个问题:垂直进度条的底部停止自动重绘。非重绘部分尺寸 = android:layout_height - android:layout_width。如果有人知道如何解决这个问题,请告诉我。
    • 如果你不关心边缘,你可以通过改变一些代码来解决这个问题:base.OnSizeChanged (h, h, oldh, oldw) 和 canvas.Translate(-Height, -6)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-22
    • 2019-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-14
    相关资源
    最近更新 更多