Android基础视图回顾

  

为了提高工作效率,对一些常见视图的特殊用法作一下总结。


一、进度条对话框

坑:https://blog.csdn.net/nailsoul/article/details/38870827 (ProgressBar占据位置但是不显示的问题)

最近我的同事孙大姐提个需求:将进度显示框的进度条放到文字上方。

1。使用系统的ProgressDialog

https://www.cnblogs.com/guop/p/5139937.html(圆形进度条与水平进度条)(注意构造方法要传主题,否则有些手机可能看不到进度条)。

看不到进度条的还有一种情况就是没有指定ProgressBar的一个属性:



注意创建ProgressDialog时不要使用建筑来创建,即:

新ProgressDialog.Builder (mContext)共创();

用这种方式创建的ProgressDialog会不显示进度条,只会显示纯文字。

原生的进度显示框设置样式为转轮,默认进度条放在文字左方,所以无法满足孙大姐的需求。

2。使用AlertDialog自定义观点

正确的用法:

progressDialog=new AlertDialog.Builder (mContext)共创();   视图rootView=LayoutInflater.from (mContext) .inflate (R.layout.mprogress_dialog, null);   pbBar=rootView.findViewById (R.id.pb_bar);   tvMsg=(TextView) rootView.findViewById (R.id.tvMsg);      progressDialog.setView (rootView);

注意错误的用法:

新ProgressDialog.Builder (mContext ProgressDialog.THEME_DEVICE_DEFAULT_DARK)共创();

上面的ProgressDialog。Builder实际上还是父类AlertDialog的类,创建出来的是AlertDialog,并非ProgressDialog,无法将AlertDialog强制转换成ProgressDialog。

也就无法使用ProgressDialog的特有方法progressDialog.setProgressStyle (ProgressDialog.STYLE_SPINNER),因此,如果想通过建筑类创建的对话框来实现圆形进度条对话框,

只有自定义。

3。继承的方式

方式1:直接继承ProgressDialog

研究ProgressDialog的源码可以发现其onCreate方法里:

其他{   视图查看=inflater.inflate (a.getResourceId (   com.android.internal.R.styleable.AlertDialog_progressLayout,   R.layout.progress_dialog),零);   mProgress=(ProgressBar) view.findViewById (R.id.progress);   mMessageView=(TextView) view.findViewById (R.id.message);   setView(查看);   }

注意上面的R是com.android.internal.R,如果将布局引用为自己应用的软件不就行了。于是继承ProgressDialog准备复写onCreate方法:

 Android基础视图回顾

可以发现会报各种引用不到的问题,因为这些变量都是父类定义的私人变量。

对于变量mContext有公共访问方法

 Android基础视图回顾

对于其他没有公共访问方法的私人变量,该如何获取呢?

答案:

还有一项就是系统资源文件com.android.internal.R如何获取呢?

答案:

com.android.internal.R是无法在java文件里进口的


将私有变量和方法用反射代替之后,代码如下:

@Override   受保护的空白   ReflectManger.setField (ProgressDialog.class,这“mProgress”,   view.findViewById (android.R.id.progress)   );   ReflectManger.setField (ProgressDialog.class,这“mMessageView”,   view.findViewById (R.id.message)   );      setView(查看);   }   a.recycle ();      如果((int) ReflectManger.getField (ProgressDialog.class,“mMax”)在0){   setMax ((int) ReflectManger.getField (ProgressDialog.class, mMax));   }      如果((int) ReflectManger.getField (ProgressDialog.class,“mProgressVal”)在0){   setProgress ((int) ReflectManger.getField (ProgressDialog.class, mProgressVal));   }      如果((int) ReflectManger.getField (ProgressDialog.class,“mSecondaryProgressVal”)在0){   setSecondaryProgress ((int) ReflectManger.getField (ProgressDialog.class, mSecondaryProgressVal));   }      如果((int) ReflectManger.getField (ProgressDialog.class,“mIncrementBy”)在0){   incrementProgressBy ((int) ReflectManger.getField (ProgressDialog.class, mIncrementBy));   }      如果((int) ReflectManger.getField (ProgressDialog.class,“mIncrementSecondaryBy”)在0){   incrementSecondaryProgressBy ((int) ReflectManger.getField (ProgressDialog.class, mIncrementSecondaryBy));   }      如果(ReflectManger.getField (ProgressDialog.class, mProgressDrawable) !=null) {   setProgressDrawable((可拉的)ReflectManger.getField (ProgressDialog.class, mProgressDrawable));   }   如果(ReflectManger.getField (ProgressDialog.class, mIndeterminateDrawable) !=null) {   setIndeterminateDrawable((可拉的)ReflectManger.getField (ProgressDialog.class, mIndeterminateDrawable));   }      如果(ReflectManger.getField (ProgressDialog.class, mMessage) !=null) {   setMessage ((CharSequence进行)ReflectManger.getField (ProgressDialog.class, mMessage));   }      setIndeterminate((布尔)ReflectManger.getField (ProgressDialog.class, mIndeterminate));   尝试{   ReflectManger.invokeMethod (ProgressDialog.class,这“onProgressChanged”,空,空);   }捕捉(NoSuchMethodException e) {   e.printStackTrace ();   }捕捉(InvocationTargetException e) {   e.printStackTrace ();   }捕捉(IllegalAccessException e) {   e.printStackTrace ();   }   super.onCreate (savedInstanceState);   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

Android基础视图回顾