android 自定义水平进度条

来源:互联网 发布:淘宝新手卖家如何推广 编辑:程序博客网 时间:2024/05/17 22:29

随着水平进度长度的变化,百分比文字也跟着进度条的长度在屏幕移动。progressbar的第一层背景要设置成透明的,第二层背景设置一种颜色;这是progressbar的样式代码:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/probardown" >
    <!-- 定义轨道的背景 -->
    <item android:id="@android:id/background">
            <shape>
                <solid android:color="@android:color/transparent" />
            </shape>
    </item>
    <!-- 定义轨道上已完成部分的样式 -->
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <solid android:color="@color/defaultgreen" />
            </shape>
        </clip>
    </item>
<item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <solid android:color="@color/greentext" />
            </shape>
        </clip>
    </item>
</layer-list>

文字怎么随着进度条在屏幕移动呢,只要动态设置距离左边的长度就可以了:

LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
p.leftMargin = -myL;
tv_text.setLayoutParams(p);
这个直接如果放在activity中是不起作用的,因为没有刷新view,所有要放在onWindowFocusChanged这个方法里执行,在view视图创建完成时,在这个方法里可以获取view的宽高和边距等;

看主要核心代码:

public void myProgress(ProgressBar progress, TextView tv_text,
int progressNum, String percentContext) {
progress.setProgress(progressNum);
tv_text.setText(" " + percentContext + "%");
int progressMarginLeft = progress.getLeft();
progressWidh = screenWidth - progressMarginLeft * 3;
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
progressWidh, 5);
params.leftMargin = iv_header.getLeft() / 2;
progress.setLayoutParams(params);
int myL = progressWidh - progressWidh * progress.getProgress() / 100;
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
p.leftMargin = -myL;
tv_text.setLayoutParams(p);
}

一定要把这个方法放在onWindowFocusChanged这里执行;

这是xml文件
 <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="10dp"
                        android:gravity="center_vertical"
                        android:orientation="horizontal" >
                        <ProgressBar
                            android:id="@+id/progress_five"
                            style="@android:style/Widget.ProgressBar.Horizontal"
                            android:layout_width="200dp"
                            android:layout_height="3dp"
                            android:layout_marginLeft="5dp"
                            android:max="100"
                            android:progress="0"
                            android:progressDrawable="@drawable/progress_drawable" />
                        <TextView
                            android:id="@+id/tv_percent_five"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="5dp"
                            android:textColor="@color/white"
                            android:textSize="@dimen/text_size_13" />
                    </LinearLayout>

这是在activity中的简单实现:

上效果图

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 没做过运营面试怎么办 苹果5s16g内存满了怎么办 618天猫有活动淘宝没有怎么办 天猫国际买到假货怎么办 天猫恶意差评怎么办 天猫删除差评被扣分了怎么办 天猫收到差评怎么办 天猫给差评骚扰怎么办 天猫客户差评怎么办 天猫没法给差评怎么办 天猫上限购一件怎么办 拍下商品不发货怎么办 天猫红包过期了怎么办 买天猫店被中介骗了钱怎么办 速卖通假货纠纷怎么办 天猫新开店被恶意拍下怎么办 新开的天猫店没有生意怎么办 银行的支票丢了怎么办 天猫积分用光了怎么办 淘宝店没有无线流量怎么办 京东卖家不发货怎么办会自动打款 一件代发顾客如果退款怎么办 京东以前的账号怎么办 旺旺对话框订单页面变宽了怎么办 阿里巴巴店铺上传图片很模糊怎么办 天猫跨店优惠券用了退货怎么办 淘宝店铺没流量没访客怎么办 京东微信和Q端黑号了怎么办? 买不了运费险了怎么办 拼多多5天不发货怎么办 天猫店手机发货成定制机怎么办 天猫退货上门取件退两件怎么办 买二手苹果手机没有账号怎么办 手机淘宝足迹不更新怎么办 淘宝申请退款不想退了怎么办 荣耀3c主板坏了怎么办 荣耀10天气删了怎么办 荣耀7i手机卡顿怎么办 荣耀v9总是自己拨号怎么办 华为荣耀5x很卡怎么办 华为荣耀6plus卡怎么办