开发者

What is the easiest way to draw a horizontal bar in Android?

开发者 https://www.devze.com 2023-01-29 18:07 出处:网络
Without using any outside APIs, how do I draw a simple green horizontal bar to depict a 开发者_开发技巧percentage? (Full being 100%)

Without using any outside APIs, how do I draw a simple green horizontal bar to depict a 开发者_开发技巧percentage? (Full being 100%)

One way I thought of was using a SeekBar but I'm not sure how to customize the height, colors, etc of it.

Is there a formula I could use to draw a rectangle on a canvas or something that would use the desired scale that I need?

For example, if I have a refrigerator that can fit 38 items, a full bar would render when there was 38 items present.


You should use ProgressBar See the documentation.

Sample code.

<ProgressBar
    android:id="@+id/progress_bar"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_margin="10dp"
    android:max="100"
    android:progress="0" />

Note this style="?android:attr/progressBarStyleHorizontal" It makes the Progress a horizontal bar instead of the spinner you were talking about. U can set the color and drawable by using android:progressDrawable="@drawable/mydrawable"


I originally thought you meant drawing a horizontal line via XML, which is easy. However, you can add a View, or TextView or whatever method (that has an XML counterpart) in your code, and set it in a loop.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号