Right to Left ProgressBar?

2019-01-14 16:25发布

Does anyone know how to make a View reversed, I have a horizontal ProgressBar and I want it to right to left instead of left to right

8条回答
走好不送
2楼-- · 2019-01-14 16:50

You don't need to rotate the entire View.

Just use a single xml attribute in your my_progress_drawable.xml:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@android:id/background"
        android:drawable="@drawable/my_background"/>
    <item android:id="@android:id/progress">
        <clip
            android:drawable="@drawable/my_right_to_left_progress"
            android:gravity="right" /> <!-- Clip the image from the RIGHT -->
    </item>

</layer-list>

The documentation tells us that gravity="right" does this:

Put the object at the right edge of its container, not changing its size. When clipOrientation is "horizontal", clipping occurs at the left side of the drawable.

Don't override onDraw(). This implementation is more stable across different versions of Android.

Unfortunately, it's impossible to set the gravity of a ClipDrawable programmatically without invoking its constructor.

查看更多
Anthone
3楼-- · 2019-01-14 16:53
<ProgressBar
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:max="100"
    android:layout_margin="8dp"
    android:layoutDirection="rtl"
    android:progress="80" />
查看更多
Anthone
4楼-- · 2019-01-14 16:58

You can flip a view in xml using scaleX or scaleY attributes

    <ProgressBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scaleX="-1"/>
查看更多
Rolldiameter
5楼-- · 2019-01-14 17:02

if you want it in XML there are two properties you can use. if you want to use android:layoutDirection="rtl" it requires minimum API 17 but if you use android:rotation="180" there is no API limitation

<ProgressBar
        android:progress="20"
        android:rotation="180"
        style="@style/Base.Widget.AppCompat.ProgressBar.Horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
查看更多
Luminary・发光体
6楼-- · 2019-01-14 17:03

Make a subclass of the normal progress bar view and implement the onDraw method to rotate the canvas before drawing it:

@Override
protected void onDraw(Canvas canvas) {
    canvas.save();
    canvas.rotate(180,<CenterX>,<CenterY>);
    super.onDraw(canvas);
    canvas.restore();
}

This should do the trick.

查看更多
家丑人穷心不美
7楼-- · 2019-01-14 17:06

Cos I'm lazy I just add these two lines to the seekbar xml:

android:layoutDirection="rtl"
android:mirrorForRtl="true"

Any downsides to this?

查看更多
登录 后发表回答