自定义进度条组合控件

package com.loaderman.progressviewdemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ProgressView pv = (ProgressView) findViewById(R.id.pv_test);
        pv.setRightText("右边信息");
        pv.setLeftText("左边信息");
        pv.setTitle("标题信息");
        pv.setProgress(75);
    }
}
package com.loaderman.progressviewdemo;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

/**
 * 
 * 自定义进度条组合控件
 */
public class ProgressView extends LinearLayout {


    TextView    tvTitle;
    ProgressBar pbProgress;
    TextView    tvLeft;
    TextView    tvRight;

    public ProgressView(Context context) {
        this(context, null);
    }

    public ProgressView(Context context, AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public ProgressView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {
        View view = View.inflate(getContext(), R.layout.layout_progress, this);
        tvLeft = (TextView) view.findViewById(R.id.tv_left);
        pbProgress = (ProgressBar) view.findViewById(R.id.pb_progress);
        tvTitle =    (TextView) view.findViewById(R.id.tv_title);
        tvRight = (TextView) view.findViewById(R.id.tv_right);
    }

    public void setTitle(String title) {
        tvTitle.setText(title);
    }

    public void setLeftText(String text) {
        tvLeft.setText(text);
    }

    public void setRightText(String text) {
        tvRight.setText(text);
    }

    public void setProgress(int progress) {
        pbProgress.setProgress(progress);
    }
}

 custom_progress.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@android:id/background"
        android:drawable="@drawable/progress_bg"/>

    <item android:id="@android:id/secondaryProgress">
        <scale
            android:drawable="@drawable/progress_progress"
            android:scaleWidth="100%"/>
    </item>

    <item android:id="@android:id/progress">
        <scale
            android:drawable="@drawable/progress_progress"
            android:scaleWidth="100%"/>
    </item>

</layer-list>

 progress_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@color/light_gray"/>
</shape>

  progress_progress.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">
    <solid android:color="#E69191"/>
</shape>

 activtiy_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.loaderman.progressviewdemo.MainActivity">

    <com.loaderman.progressviewdemo.ProgressView
        android:id="@+id/pv_test"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
</RelativeLayout>

 layout_progress.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="3dp">

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginRight="5dp"
        android:textColor="@color/black"
        android:textSize="16sp"
        />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="25dp">

        <ProgressBar
            android:id="@+id/pb_progress"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:progress="50"
            android:progressDrawable="@drawable/custom_progress"/>

        <TextView
            android:id="@+id/tv_left"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="5dp"
            android:textColor="@color/black"
            android:textSize="16sp"
            />

        <TextView
            android:id="@+id/tv_right"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="5dp"
            android:textColor="@color/black"
            android:textSize="16sp"
            />

    </RelativeLayout>

</LinearLayout>

 效果图:



 

原文地址:https://www.cnblogs.com/loaderman/p/6491329.html