android 实现垂直的ProgressBar

I had recently come across the need for a vertical progress bar but was unable to find a solution using the existing Progress Bar widget. The solutions I came across generally required an extension of the current Progress Bar or a completely new class in it self. I wasn‘t convinced rolling out a new class to achieve a simple orientation change was necessary.

This article presents a simple, elegant, and most importantly, a no-hack solution to achieving a vertical progress bar. I‘m going to skip the explanation and simply provide a cookie cutter solution. If you require further details feel free to contact me or leave a comment below.

Create an xml in your drawable folder (not drawable-hdpi or drawable-mdpi -- place it in drawable). For this example I call my xml vertical_progress_bar.xml

Here‘s what to place in the xml file:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:id="@android:id/background">
    <shape>
      <corners android:radius="5dip" />
      <gradient
        android:startColor="#ff9d9e9d"
        android:centerColor="#ff5a5d5a"
        android:centerY="0.75"
        android:endColor="#ff747674"
        android:angle="180"
      />
    </shape>
  </item>
  <item android:id="@android:id/secondaryProgress">
    <clip android:clipOrientation="vertical" android:gravity="bottom">
      <shape>
        <corners android:radius="5dip" />
        <gradient
          android:startColor="#80ffd300"
          android:centerColor="#80ffb600"
          android:centerY="0.75"
          android:endColor="#a0ffcb00"
          android:angle="180"
        />
      </shape>
    </clip>
  </item>
  <item android:id="@android:id/progress">
    <clip android:clipOrientation="vertical" android:gravity="bottom">
      <shape>
        <corners android:radius="5dip" />
        <gradient
          android:startColor="#ffffd300"
          android:centerColor="#ffffb600"
          android:centerY="0.75"
          android:endColor="#ffffcb00"
          android:angle="180"
        />
      </shape>
    </clip>
</item>

Create an xml file called styles.xml and place it in res/values. If your project already contains styles.xml in res/values then skip this step.

Modify your styles.xml file and append the following code to the end of the file:

<style name="Widget">
</style>
<style name="Widget.ProgressBar">
  <item name="android:indeterminateOnly">true</item>
  <item name="android:indeterminateBehavior">repeat</item>
  <item name="android:indeterminateDuration">3500</item>
  <item name="android:minWidth">48dip</item>
  <item name="android:maxWidth">48dip</item>
  <item name="android:minHeight">48dip</item>
  <item name="android:maxHeight">48dip</item>
</style>
<style name="Widget.ProgressBar.Vertical">
  <item name="android:indeterminateOnly">false</item>
  <item name="android:progressDrawable">@drawable/progress_bar_vertical</item>
  <item name="android:indeterminateDrawable">@android:drawable/progress_indeterminate_horizontal</item>
  <item name="android:minWidth">1dip</item>
  <item name="android:maxWidth">12dip</item>
</style>

Add your new vertical progress bar to your layout. Here‘s an example:

<ProgressBar
  android:id="@+id/vertical_progressbar"
  android:layout_width="12dip"
  android:layout_height="300dip"
  style="@style/Widget.ProgressBar.Vertical"
/>

That should be all you need to do to make use of a vertical progress bar in your project. Optionally, you might have custom drawable nine-patch images that you are using for the progress bar. You should make the appropriate changes in the progress_bar_vertical.xml file. I hope this helps you out in your project!

by jagsaund from http://stackoverflow.com

原文地址:http://stackoverflow.com/questions/3926395/android-set-a-progressbar-to-be-a-vertical-bar-instead-of-horizontal

这个解决方案算是比较好的,个人认为比自己重写个垂直类要好,因为当我用旋转得来的重写类时发现部分手机无法预期显示的问题。

时间: 2024-10-14 04:34:17

android 实现垂直的ProgressBar的相关文章

Android下如何自定义ProgressBar的外观

在Android应用中经常会用的到一些进度条,这些进度条的样子千差万别,但是大多都是由ProgressBar来的.但是Android系统自带的进度条样式却不太好看,今天要做的就是自定义一个好看的ProgressBar. 我们先来看看Android自带的进度条的样子: 我们今天的目标,自定义 的进度条的样子: 不难发现差距还是挺大的,好了,废话不多说,进入正题: 我们首先还是从源码入手,我们可以打开ADT目录下的sdk\platforms\android-16\data\res\values文件夹

Android学习笔记_76_Android ProgressBar 进度条

android 进度条的样式  例1:(默认样式(中等圆形))Xml代码 <ProgressBar      android:id="@+id/progressBar1"     android:layout_width="wrap_content"      android:layout_height="wrap_content"      /> 例2:(超大圆形)Xml代码 <ProgressBar      android

Android ListView,GridView,ScrollView,ProgressBar,SeekBar,RelativeLayout,EditText常用属性

ListView的一些特殊属性: 1.ListView的XML属性 [java] view plaincopy android:divider//在列表条目之间显示的drawable或color android:dividerHeight//用来指定divider的高度 android:entries//构成ListView的数组资源的引用.对于某些固定的资源,这个属性提供了比在程序中添加资源更加简便的方式 android:footerDividersEnabled//当设为false时,Lis

Android自定义横向的ProgressBar

其实很简单,上代码! 在res下创建drawable文件夹,新建文件drawable/progressbar_color.xml <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >     <!-- 背景  gradient是渐变,corners定

Android开发自学笔记(Android Studio)—4.5 ProgressBar及其子类

一.前言 ProgressBar本身代表了进度条组件,它还派生出了两个常用的组件:SeekBar和RatingBar,他们的使用方法类似,只是显示界面有一定的区别.我们看一下API文档中的说明: 从图中我们是可以直接看到ProgressBar继承自View类,直接子类有AbsSeekBar和ContentLoadingProgressBar,间接子类有RatingBar和SeekBar.下面依次介绍一下这几个控件的使用方法. 二.ProgressBar ProgressBar是界面用于与用户交互

Android 学习心得(6)——ProgressBar(进度条)

没什么技术含量就是对系统进度条的简单应用 贴上代码 1 package cn.bwl.progressbar; 2 3 import android.support.v7.app.ActionBarActivity; 4 import android.view.View; 5 import android.widget.Button; 6 import android.widget.ProgressBar; 7 import android.widget.TextView; 8 import a

Android开发自学笔记(Android Studio)&mdash;4.5 ProgressBar及其子类

一.前言 ProgressBar本身代表了进度条组件,它还派生出了两个常用的组件:SeekBar和RatingBar,他们的使用方法类似,只是显示界面有一定的区别.我们看一下API文档中的说明: 从图中我们是可以直接看到ProgressBar继承自View类,直接子类有AbsSeekBar和ContentLoadingProgressBar,间接子类有RatingBar和SeekBar.下面依次介绍一下这几个控件的使用方法. 二.ProgressBar ProgressBar是界面用于与用户交互

Android学习笔记之ProgressBar案例分析

(1) <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddi

android常用组件之ProgressBar

ProgressBar组件是无法拖动的进度条,他能够显示当前下载进度,他含有一个次进度条,比如播放流媒体时显示的缓冲进度. 该实例是通过四个按键分别控制主进度条和次进度条的增减. 首先上布局文件代码: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     and