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中的简单实现:
上效果图
- android 自定义水平进度条
- Android自定义水平进度条
- Android自定义水平进度条控件
- Android自定义水平渐变进度条
- Android ProgressBar高级自定义水平/圆形进度条
- android自定义view实现水平进度条
- 自定义水平进度条
- 自定义水平进度条
- 自定义水平进度条
- Android自定义View:水平带数字百分比的进度条
- Android 自定义水平进度条的圆角进度
- Android自定义View分享——一个水平的进度条
- android水平进度条布局
- Android--水平进度条对话框
- 简单的自定义水平进度条
- 自定义水平进度条Horizontal ProgressBar
- 自定义水平进度条(颜色)
- 自定义进度条(水平,圆形)
- CSS盒模型
- Android 如何让EditText不自动获取焦点
- ios 移除 栈中的 uiviewcontroller ,直接返回之前的某个controller
- codeforces-580B
- (一)ActiveMQ 简介 && 环境搭建
- android 自定义水平进度条
- MySQL锁定状态查看命令
- YUM安装PHP5.5及phpredis扩展
- Linux 启动流程即init程序分析--1
- JDK,JRE,JVM区别与联系
- Android获取当前位置信息,百分之百有效
- 【转】LinkedList、ArrayList各自的使用场景分析
- *** Collection <__NSArrayM: 0x7f9fb3c48bc0> was mutated while being enumerated问题
- Android开发中遇到的一些小问题