以编程方式创建可绘制的进度(Create a progress drawable programmatically)
我有一个场景,我需要有大量的进度条可绘制。 我不能创建所有这些XML资源,因为我想让用户选择一种颜色,然后将用于动态创建绘图。 下面是XML中的一个这样的drawable,我如何以编程方式创建这个精确的drawable?
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@android:id/background"> <shape> <solid android:color="@color/transparent" /> <stroke android:width="2px" android:color="@color/category_blue_stroke"/> </shape> </item> <item android:id="@android:id/progress"> <clip> <shape> <solid android:color="@color/category_blue" /> <stroke android:width="2px" android:color="@color/category_blue_stroke"/> </shape> </clip> </item> </layer-list>
I have a scenario where i need to have a large number of progress bar drawables. I cant create xml resources for all of them because i want the user to choose a color that will then be used to dynamically create the drawable. Below is one such drawable in xml, how can i create this exact drawable programmatically?
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@android:id/background"> <shape> <solid android:color="@color/transparent" /> <stroke android:width="2px" android:color="@color/category_blue_stroke"/> </shape> </item> <item android:id="@android:id/progress"> <clip> <shape> <solid android:color="@color/category_blue" /> <stroke android:width="2px" android:color="@color/category_blue_stroke"/> </shape> </clip> </item> </layer-list>
原文:https://stackoverflow.com/questions/18011075
最满意答案
你的ProgressDialog不应该是intendminate。 您应该使用
false
而不是true
调用setIndeterminate
pd.setIndeterminate(false);
你应该使用
setProgress
而不是incrementProgressBy
your ProgressDialog should not be intederminate. You should call
setIndeterminate
withfalse
instead oftrue
pd.setIndeterminate(false);
and you should use
setProgress
instead ofincrementProgressBy
相关问答
更多-
/// try :) progressgialog.post(new Runnable(){ run () { progressgialog.setMessage("asd"); } }) //// catch :) activity.runOnUiThread(new Runnable() { progressgialog.setMessage("asd"); }); I found a work-around and thought I would share it. Since I was m ...
-
首先,如果你可以替换它会更有意义: pro_dialog.incrementProgressBy(2); 有了这个: pro_dialog.setProgress(progress); 当前代码在进度变量达到MAX_PROGRESS后解除对话框,但进度对话框不显示此变量的当前值。 如果您想显示AlertDialog,可以在“pro_dialog.dismiss()”之后执行此操作。 案例进展对话框样式已在此处讨论过 。 First of all, it would make more sense if ...
-
Android进度对话框只更新消息,而不是栏和行(Android progress Dialog only updating Message , not the bar and the lines)[2022-06-28]
你的ProgressDialog不应该是intendminate。 您应该使用false而不是true调用setIndeterminate pd.setIndeterminate(false); 你应该使用setProgress而不是incrementProgressBy your ProgressDialog should not be intederminate. You should call setIndeterminate with false instead of true pd.setInd ... -
更新进度对话框(Updating progress dialog)[2022-06-26]
第二个示例可能运行得更快,但它独占GUI线程。 使用AsyncTask的第一种方法更好; 它允许GUI在下载过程中保持响应。 我发现将AsyncTask与SwingWorker进行比较很有帮助,如本例所示。 The second example may run faster, but it monopolizes the GUI thread. The first approach, using AsyncTask, is better; it allows the GUI to stay responsi ... -
您可以使用onProgressUpdated()来更改消息,因为此方法在UI线程上运行。 但是,它不会让您有机会在特定的时间内更改消息。 You can use onProgressUpdated() in order to change the message, because this method is run on the UI Thread. Though, it doesn't give you the opportunity to change the message on the parti ...
-
你不必做任何特别棘手或不寻常的事情。 只需使用DialogBox()创建模式对话框即可。 在对话框过程的WM_INITDIALOG处理程序中,创建后台线程以加载文件。 随着加载的进行,将PBM_SETPOS消息发送到进度条控件以进行更新。 加载完成后,调用EndDialog()关闭对话框。 但是,必须在对话框过程中EndDialog() 。 所以要做到这一点,你需要发送一个虚拟消息(例如WM_APP ): DialogBox(..., DlgProc); // File loading is done an ...
-
Bala您创建的自定义布局作为对话框插入水平progressBar的代码,其中水平SCrollBar的颜色为绿色, 并带有自定义颜色。 Bala the custom layout which you have created as a dialog insert the code for the horizontal progressBar having color green from Horizontal SCrollBar with Custom Color.
-
Swing中的进度对话框(Progress Dialog in Swing)[2021-07-11]
您可能想看看ProgressMonitor 。 如果操作需要很长时间,它会自动弹出一个带进度条的对话框; 请参阅如何使用进度监视器 。 You might want to look into ProgressMonitor. It automatically pops up a dialog with a progress bar if the operation is taking a long time; see How to Use Progress Monitors. -
调用publishProgress以在后台计算仍在运行时在UI线程上发布更新。 您可以在特定时间间隔内调用它来发布更新,以便通知用户正在运行的任务。 典型案例如下: protected Void doInBackground(String.. params ) { publishProgress("sentence 1"); //do some time consuming task //.. publishProgress("sentence 2"); ...
-
覆盖AsyncTask为您提供的onProgressUpdate方法,不要创建自己的方法。 另外,不要自己调用onProgressUpdate ! 这是一个回调方法,系统使用该方法为您提供UI线程的回调。 如果直接调用它,代码将在后台线程中运行,您无法更新UI。 相反,使用publishProgress 。 编辑:为了能够将正确的值传递给publishProgress ,您还需要在扩展AsyncTask时指定泛型类型(第二个泛型类型是进度的类型)。 试试这样: private static class My ...