What is the easiest way to draw a horizontal bar in Android?
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.
精彩评论