Android系统的进度条控件默许的设计的不是很周全,比方没有包含文字的显现,那么如何在Android进度条控件上显
示文字呢?来自Google内部的代码来理解下,主要运用的addView这样的办法经过掩盖一层Chronometer秒表控件来完成,
整个代码如下
publicclassTextProgressBarextendsRelativeLayoutimplementsOnChronometerTickListener{
publicstaticfinalStringTAG=”TextProgressBar”;
staticfinalintCHRONOMETER_ID=android.R.id.text1;
staticfinalintPROGRESSBAR_ID=android.R.id.progress;
ChronometermChronometer=null;
ProgressBarmProgressBar=null;
longmDurationBase=-1;
intmDuration=-1;
booleanmChronometerFollow=false;
intmChronometerGravity=Gravity.NO_GRAVITY;
publicTextProgressBar(Contextcontext,AttributeSetattrs,intdefStyle){
super(context,attrs,defStyle);
}
publicTextProgressBar(Contextcontext,AttributeSetattrs){
super(context,attrs);
}
publicTextProgressBar(Contextcontext){
super(context);
}//Android开发网提示关键局部在这里
@Override
publicvoidaddView(Viewchild,intindex,ViewGroup.LayoutParamsparams){
super.addView(child,index,params);
intchildId=child.getId();
if(childId==CHRONOMETER_ID&&childinstanceofChronometer){
mChronometer=(Chronometer)child;
mChronometer.setOnChronometerTickListener(this);
//CheckifChronometershouldmovewithwithProgressBar
mChronometerFollow=(params.width==ViewGroup.LayoutParams.WRAP_CONTENT);
mChronometerGravity=(mChronometer.getGravity()&Gravity.HORIZONTAL_GRAVITY_MASK);
}elseif(childId==PROGRESSBAR_ID&&childinstanceofProgressBar){
mProgressBar=(ProgressBar)child;
}
}
@android.view.RemotableViewMethod
publicvoidsetDurationBase(longdurationBase){
mDurationBase=durationBase;
if(mProgressBar==null||mChronometer==null){
thrownewRuntimeException(“ExpectingchildProgressBarwithid”+
“‘android.R.id.progress’andChronometerid’android.R.id.text1′”);
}
//UpdatetheProgressBarmaximumrelativetoChronometerbase
mDuration=(int)(durationBase-mChronometer.getBase());
if(mDuration<=0){
mDuration=1;
}
mProgressBar.setMax(mDuration);
}
publicvoidonChronometerTick(Chronometerchronometer){
if(mProgressBar==null){
thrownewRuntimeException(
“ExpectingchildProgressBarwithid’android.R.id.progress’”);
}
//StopChronometerifwe’repastduration
longnow=SystemClock.elapsedRealtime();
if(now>=mDurationBase){
mChronometer.stop();
}intremaining=(int)(mDurationBase-now);
mProgressBar.setProgress(mDuration-remaining);
if(mChronometerFollow){
RelativeLayout.LayoutParamsparams;
params=(RelativeLayout.LayoutParams)mProgressBar.getLayoutParams();
intcontentWidth=mProgressBar.getWidth()-(params.leftMargin+params.rightMargin);
intleadingEdge=((contentWidth*mProgressBar.getProgress())/
mProgressBar.getMax())+params.leftMargin;
intadjustLeft=0;
inttextWidth=mChronometer.getWidth();
if(mChronometerGravity==Gravity.RIGHT){
adjustLeft=-textWidth;
}elseif(mChronometerGravity==Gravity.CENTER_HORIZONTAL){
adjustLeft=-(textWidth/2);
}
leadingEdge+=adjustLeft;
intrightLimit=contentWidth-params.rightMargin-textWidth;
if(leadingEdge<params.leftMargin){
leadingEdge=params.leftMargin;
}elseif(leadingEdge>rightLimit){
leadingEdge=rightLimit;
}
params=(RelativeLayout.LayoutParams)mChronometer.getLayoutParams();
params.leftMargin=leadingEdge;
mChronometer.requestLayout();
}
}
}
1、IT大王遵守相关法律法规,由于本站资源全部来源于网络程序/投稿,故资源量太大无法一一准确核实资源侵权的真实性;
2、出于传递信息之目的,故IT大王可能会误刊发损害或影响您的合法权益,请您积极与我们联系处理(所有内容不代表本站观点与立场);
3、因时间、精力有限,我们无法一一核实每一条消息的真实性,但我们会在发布之前尽最大努力来核实这些信息;
4、无论出于何种目的要求本站删除内容,您均需要提供根据国家版权局发布的示范格式
《要求删除或断开链接侵权网络内容的通知》:https://itdw.cn/ziliao/sfgs.pdf,
国家知识产权局《要求删除或断开链接侵权网络内容的通知》填写说明: http://www.ncac.gov.cn/chinacopyright/contents/12227/342400.shtml
未按照国家知识产权局格式通知一律不予处理;请按照此通知格式填写发至本站的邮箱 wl6@163.com