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