【问题标题】:Right to Left ProgressBar?从右到左进度条?
【发布时间】:2011-04-22 03:01:45
【问题描述】:

有谁知道如何使视图反转,我有一个水平的 ProgressBar,我希望它从右到左而不是从左到右

【问题讨论】:

  • 什么语言?什么平台?什么 API?
  • 为什么需要 RTL 进度条?我看到的所有进度条都是从左到右的。
  • 我们只能用伪代码回答伪代码问题……否则请至少说明 EboMike 的评论要求什么。
  • 哦,对不起,我发誓我把 Android。只是为了游戏。

标签: android view progress-bar


【解决方案1】:

这更容易。您可以简单地调用以下方法,然后将其旋转并按您想要的方式工作。

progressBar.setRotation(180);

一个例子:

【讨论】:

  • 应该是目前最好的答案
  • 您可以直接在 XML 文件中使用android:rotation="180"进行设置
  • 它也会使我的应用程序崩溃。安卓6.0。默认方向是 RTL,但我想要进度条 LTR。
  • 在 ProgressBar 对象上设置LayoutDirection 对我有用。
  • 在 API 24 (7.0) 中,seekbar 变得透明,rotation=180
【解决方案2】:

您可以使用 scaleX 或 scaleY 属性在 xml 中翻转视图

    <ProgressBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scaleX="-1"/>

【讨论】:

  • 绝对应该是答案
  • 在圆形进度条中可用
  • 哇!!什么是黑客!谢谢!
【解决方案3】:

创建一个普通进度条视图的子类,并实现onDraw方法在绘制之前旋转画布:

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

这应该可以解决问题。

【讨论】:

    【解决方案4】:
     public class inverseSeekBar extends ProgressBar {
    
    public inverseSeekBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }
    
    public inverseSeekBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }
    
    public inverseSeekBar(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }
    
    @Override
    protected synchronized void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
         canvas.save(); 
    
            //now we change the matrix
            //We need to rotate around the center of our text
            //Otherwise it rotates around the origin, and that's bad. 
            float py = this.getHeight()/2.0f;
            float px = this.getWidth()/2.0f;
            canvas.rotate(180, px, py); 
    
            //draw the text with the matrix applied. 
            super.onDraw(canvas); 
    
            //restore the old matrix. 
            canvas.restore(); 
    }}
      <com.hlidskialf.android.widget.inverseSeekBar 
    
           style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="200dip"
        android:layout_height="wrap_content"
        android:max="100"
        android:progress="50"
        android:secondaryProgress="75" 
    
    /> 
    

    我的包:com.test.testProgressBar

    【讨论】:

      【解决方案5】:

      您无需旋转整个View

      只需在您的my_progress_drawable.xml 中使用a single xml attribute

      <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 告诉我们gravity="right" 这样做:

      将对象放在其容器的右边缘,不改变其大小。当clipOrientation 为“水平”时,裁剪发生在drawable 的左侧。

      不要覆盖onDraw()。此实现在不同版本的 Android 上更加稳定。

      不幸的是,如果不调用 constructor,就无法以编程方式设置 ClipDrawable 的重力。

      【讨论】:

      • 对于使用 而不是 的全息主题,可以使用 scaleGravity 代替
      【解决方案6】:

      如果你想在 XML 中使用它,你可以使用两个属性。 如果你想使用 android:layoutDirection="rtl" 它需要最低 API 17 但如果你使用 android:rotation="180" 则没有 API 限制

      <ProgressBar
              android:progress="20"
              android:rotation="180"
              style="@style/Base.Widget.AppCompat.ProgressBar.Horizontal"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"/>
      

      【讨论】:

        【解决方案7】:

        因为我很懒,我只是将这两行添加到seekbar xml:

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

        这样做有什么缺点吗?

        【讨论】:

        • mirrorforRtl 只支持 api > 18
        • 如果有人想在 rtl 和 ltr 布局之间切换,这一定是正确的答案。如果你想要 LTR 的进度应该从左边开始,而 RTL 的进度应该从右边开始,只需添加 -> android:mirrorForRtl="true"
        【解决方案8】:
        <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" />
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-12-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-07-22
          相关资源
          最近更新 更多