更改进度条android的半透明背景颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23869248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing translucent background color of progressbar android
提问by Niranjan Balkrishna Prajapati
I want to change background color of the progressbar having black translucent background with opacity less than 50%. I searched a lot about this but not able to find solution. I don't know whether i'm right or not but it has some thing to do with mdecor property of the progressbar.
我想更改具有黑色半透明背景且不透明度小于 50% 的进度条的背景颜色。我对此进行了很多搜索,但无法找到解决方案。我不知道我是否正确,但这与进度条的 mdecor 属性有关。
Following is the code I am using it.
以下是我正在使用它的代码。
pd = ProgressDialog.show(getActivity(), null, null,true);
pd.setContentView(R.layout.remove_border);
The code for remove border layout is:
移除边框布局的代码是:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:orientation="vertical" >
<ProgressBar
android:id="@+id/progressBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@android:color/transparent"
android:indeterminateDrawable="@drawable/my_progress_indeterminate" />
</LinearLayout>
The code for my_progress_indeterminate is:
my_progress_indeterminate 的代码是:
<animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/loadingiconblack"
android:pivotX="50%"
android:pivotY="50%" />
Following is the image the background area is marked in red circle. I want to change color to white with same opacity.
以下是背景区域用红色圆圈标记的图像。我想将颜色更改为具有相同不透明度的白色。
EDIT:
编辑:
Final solution to my issue thanks to Sripathi and user3298272 combining both solutions here is my updated answer:
感谢 Sripathi 和 user3298272 结合这两种解决方案,我的问题的最终解决方案是我的更新答案:
pd = new Dialog(this, android.R.style.Theme_Black);
View view = LayoutInflater.from(this).inflate(R.layout.remove_border, null);
pd.requestWindowFeature(Window.FEATURE_NO_TITLE);
pd.getWindow().setBackgroundDrawableResource(R.color.transparent);
pd.setContentView(view);
pd.show();
remove_border xml code: Here android:gravity = "center" is added in linear layout code of mine.
remove_border xml 代码:这里 android:gravity = "center" 是在我的线性布局代码中添加的。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:gravity="center"
android:orientation="vertical" >
<ProgressBar
android:id="@+id/progressBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@android:color/transparent"
android:indeterminateDrawable="@drawable/my_progress_indeterminate" />
</LinearLayout>
color xml code:
颜色xml代码:
<resources>
<color name="transparent">#80FFFFFF</color>
</resources>
In #80FFFFFF 80 is the opacity value 50% which i took from user3298272.
#80FFFFFF 80 是我从 user3298272 获取的不透明度值 50%。
采纳答案by Sripathi
You can use dialog instead of progressdialog,
您可以使用对话框而不是进度对话框,
dialogTransparent = new Dialog(this, android.R.style.Theme_Black);
View view = LayoutInflater.from(getActivity()).inflate(
R.layout.remove_border, null);
dialogTransparent.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialogTransparent.getWindow().setBackgroundDrawableResource(
R.color.transparent);
dialogTransparent.setContentView(view);
Update:
更新:
Here the color transparent is #80FFFFFF
回答by Chandru
Here is the transparent color ratio for the Hex color codes.
100% — FF
95% — F2
90% — E6
85% — D9
80% — CC
75% — BF
70% — B3
65% — A6
60% — 99
55% — 8C
50% — 80
45% — 73
40% — 66
35% — 59
30% — 4D
25% — 40
20% — 33
15% — 26
10% — 1A
5% — 0D
0% — 00
Just add the percentage of opacity before the color code. For example: color code for black is #0000. If i want transparent black with 50%transparency means just add 80before #0000(i.e. #800000)
这是十六进制颜色代码的透明颜色比率。
100% — FF
95% — F2
90% — E6
85% — D9
80% — CC
75% — BF
70% — B3
65% — A6
60% — 99
55% — 8C
50% — 80
45% — 73
40% — 66
35% — 59
30% — 4D
25% — 40
20% — 33
15% — 26
10% — 1A
5% — 0D
0% — 00
只需在颜色代码之前添加不透明度百分比。例如:黑色的颜色代码是#0000。如果我想要具有50%透明度的透明黑色意味着只需在 #0000(即#800000)之前添加80
回答by rnofenko
Just set progressBar.setAlpha(0.2f);
只需设置 progressBar.setAlpha(0.2f);