Android ProgressBar UI custom layout

笑着哭i 提交于 2019-11-26 12:23:18

问题


I am trying to customize a status bar so it will look like this image:\"enter

However, after a good couple of hours all I have is a status bar with the background I need only (Image 2): \"enter

The current code I have is: xml layout:

<ProgressBar
                android:id=\"@+id/pBarOverallStatus\"
                style=\"?android:attr/progressBarStyleHorizontal\"
                android:layout_width=\"0dip\"
                android:layout_height=\"fill_parent\"
                android:layout_marginLeft=\"10dip\"
                android:layout_marginRight=\"10dip\"
                android:layout_weight=\"94\"
                android:indeterminateOnly=\"false\"
                android:max=\"100\"
                android:progressDrawable=\"@drawable/progress_bar_states\" >
            </ProgressBar>

The progress_bar_states.xml:

<layer-list xmlns:android=\"http://schemas.android.com/apk/res/android\" >

    <!-- <item android:id=\"@android:id/progress\">
        <bitmap
            android:gravity=\"center\"
            android:src=\"@drawable/progressbar_progressing\" />

        <corners android:radius=\"10dp\" />
    </item>
     -->

</layer-list>

Removing the comment, where proressbar_progressing looks like \"enter then I am having something a bit ugly as there are no corners. \"enter

I am adding the background from code, something like:

overallStatus = (ProgressBar) findViewById(R.id.pBarOverallStatus);
Resources res = getResources();
overallStatus.setBackgroundDrawable(res.getDrawable(R.drawable.progressbar_background));
overallStatus.setProgress(50);

I tried to add corners to the image but no luck. Does anybody have any idea what I am doing wrong here? Or what am I missing? Also, do you have any idea how could I add the left and right rings? The resource: \"enter


回答1:


The solution (feels weird answering your own): First, one problem was that progress drawable had a different size than background (stupid me!). Also, for progress drawable a clip xml was needed. Something like progressbar_progress_clip.xml:

<?xml version="1.0" encoding="utf-8"?>
<clip
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/progressbar_progressing"
    android:clipOrientation="horizontal"
    android:gravity="left"/>

Then, add the progress bar and two images in a relative layout, so that the images can be drawn after the status bar. Something like this:

<RelativeLayout
                android:id="@+id/relativeLayout1"
                android:layout_width="0dip"
                android:layout_height="fill_parent"
                android:layout_marginLeft="10dip"
                android:layout_marginRight="10dip"
                android:layout_weight="94" >

                <ProgressBar
                    android:id="@+id/pBarOverallStatus"
                    style="?android:attr/progressBarStyleHorizontal"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_marginBottom="2dp"
                    android:layout_marginTop="2dp"
                    android:indeterminateOnly="false"
                    android:max="100"
                    android:progressDrawable="@drawable/progressbar_progress_clip" >
                </ProgressBar>

                <ImageView
                    android:id="@+id/ringLeft"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:layout_alignParentLeft="true"
                    android:layout_centerVertical="true"
                    android:layout_marginLeft="7dp"
                    android:contentDescription="@string/status_bar_ring"
                    android:src="@drawable/status_bar_ring" />

                <ImageView
                    android:id="@+id/ringRight"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true"
                    android:layout_marginRight="7dp"
                    android:contentDescription="@string/status_bar_ring"
                    android:src="@drawable/status_bar_ring" />
            </RelativeLayout>

Thanks, guys!




回答2:


First create a new drawable XML file:

<?xml version="1.0" encoding="UTF-8"?> 
<shape
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 

    <corners
        android:bottomRightRadius="7dp"
        android:bottomLeftRadius="7dp"
        android:topLeftRadius="7dp"
        android:topRightRadius="7dp"/> 

</shape> 

Then in your layout, add the following attribute to your progress bar:

android:background="@drawable/the_file_you_previously_created"




回答3:


You could make a FrameLayout with an ImageView holding your drawable and set its alpha value to a low value. Then create a ProgressBar with a ClipDrawable in the same FrameLayout. And set the value of ClipDrawable in your progress update.

Code:

<FrameLayout
        android:layout_gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    <ProgressBar
        android:max="100"
        android:id="@+id/progress_bar"
        android:layout_gravity="center"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:progressDrawable="@drawable/custom_progress_image"
        android:layout_marginRight="5dp" />

        <ImageView
            android:alpha="0.4"
            android:src="@drawable/app_logo"
            android:layout_gravity="center"
            android:layout_width="200dp"
            android:layout_height="200dp" />
</FrameLayout> 


来源:https://stackoverflow.com/questions/9921621/android-progressbar-ui-custom-layout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!