android 實現垂直的ProgressBar

來源:互聯網
上載者:User

標籤:

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

 

這個解決方案算是比較好的,個人認為比自己重寫個垂直類要好,因為當我用旋轉得來的重寫類時發現部分手機無法預期顯示的問題。

android 實現垂直的ProgressBar

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.