使用Progress
控件很方便,但是如果有居中需求的话,某些场景会增加一层布局嵌套
而且每个布局都要书写,很麻烦
而使用ProgressDialog
书写起来很方便,但是某些场景不能很直接的提供正确的上下文.(容器不是activity
或 fragment
时)
各位大哥在实际开发中如何实现进度条的需求的?
//我尝试这样操作的情况,下 在app第一次进入的时候,我点击按钮向容器上添加progressbar的时候,这个progressbar是不会旋转的,卡在界面上
//而关闭这个activity后(app中还有activity存在),再次进入activity 继续添加,这时候progressbar就会旋转了,这是为什么呢?
ProgressBar progressBar = new ProgressBar(this);
ViewGroup decorView = (ViewGroup) getWindow().getDecorView();
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER);
progressBar.setLayoutParams(layoutParams);
decorView.addView(progressBar);
天蓬老师2017-04-18 09:18:17
I can’t give you any constructive advice on this. Sometimes to save trouble, I will write an XML interface for ProgressBar, inflate it when using it and insert it directly into the rootView. If you like, I can re-implement this logic in BaseActivity. One layer of encapsulation can basically meet some simple needs.
To make it more complicated, let the progressBar interact with the user's operations. For example, if an editing interface has an entrance for uploading pictures, we can change the submit key to the Progress Widget while the user is waiting for the upload after selecting the picture. Blocking user operations can also effectively prompt progress.
As for ProgressDialog, I think it is too blocking. Except in some scenarios with strong logical order, such as when certain files must be loaded before proceeding to the next step, this is generally not recommended.
PHP中文网2017-04-18 09:18:17
As long as you can get it
Context
, 那就接着用ProgressDialog
Take Application
as an example:
Write an Activity as a proxy for Dialog popup
startActivity(Intent)
天蓬老师2017-04-18 09:18:17
I’m actually more interested in the question of what kind of scenario the “context is not provided correctly” in the question is. What situations did the questioner encounter that he couldn't get? progressbar
可以拿到上下文而progressdialog
巴扎黑2017-04-18 09:18:17
If it is not a container of activity or fragment, then this container must be in activity or fragment when it is created.
If it is not, then its parent container or parent container must be in activity or fragment.
In this case, then you can pass the context level by level through the constructor, right?