Android - ProgressBar setVisibility to GONE 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24342875/
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
Android - ProgressBar setVisibility to GONE not working
提问by j.grima
I've been adding a ProgressBar
to the fragments in my app. I've set it up to the two main fragments (used as tabs) as follows:
我一直在向ProgressBar
我的应用程序中的片段添加一个。我已将其设置为两个主要片段(用作选项卡),如下所示:
ProgressBar
in activity_main.xml
:
ProgressBar
在activity_main.xml
:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
</RelativeLayout>
Setting ProgressBar
VISIBLE
and GONE
:
设置ProgressBar
VISIBLE
和GONE
:
spinner = (ProgressBar)getActivity().findViewById(R.id.progressBar1);
spinner.setVisibility(View.VISIBLE);
spinner.setVisibility(View.GONE);
This works without any problems. I've tried to add another ProgressBar
to another fragment which has a WebView
:
这没有任何问题。我试图将另一个添加ProgressBar
到另一个片段中,该片段具有WebView
:
ProgressBar
in fragment_article.xml
:
ProgressBar
在fragment_article.xml
:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="info.androidhive.slidingmenu.ArticleFragment$PlaceholderFragment" >
<WebView android:id="@+id/webPage"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
</RelativeLayout>
Setting Visibility:
设置可见性:
spinner = (ProgressBar)getActivity().findViewById(R.id.progressBar1);
spinner.setVisibility(View.VISIBLE);
spinner.setVisibility(View.GONE);
Setting the visibility the same way as the previous code but for some reason this is not setting the ProgressBar
to GONE
. Not sure what's wrong.
设置知名度方式与前面的代码相同的,但由于某种原因,这是不设置ProgressBar
到GONE
。不知道出了什么问题。
I've tried using clearAnimation
as suggested here Android, setVisbility to gone not working in RelativeLayoutbut still nothing.
我试过clearAnimation
按照这里的建议使用Android,setVisbility 在 RelativeLayout 中不起作用,但仍然没有。
spinner.clearAnimation();
spinner.setVisibility(View.GONE);
采纳答案by Illegal Argument
Check this code:
检查此代码:
spinner = (ProgressBar)getActivity().findViewById(R.id.progressBar1);
If you are using fragments it should be like this:
如果您使用片段,它应该是这样的:
spinner = (ProgressBar)viewIinflated.findViewById(R.id.progressBar1);//same case with dialogs
If you are using activity then:
如果您正在使用活动,则:
spinner = (ProgressBar)findViewById(R.id.progressBar1);
回答by KevinRyu
I had a same issue (progressBar.setVisibility() was not working).
我有同样的问题(progressBar.setVisibility() 不起作用)。
As @Illegal Argument said,
正如@Illegal Argument 所说,
// in Activity
ProgressBar mProgressBar = (ProgressBar)findViewById(R.id.progressBar1);
mProgressBar.setVisibility(View.GONE);
should be working, if that code runs on uiThread(mainThread).
如果该代码在 uiThread(mainThread) 上运行,则应该可以正常工作。
My problem was that I was trying to run the code not on uiThread. So I solved the issue by changing code from
我的问题是我试图在 uiThread 上运行代码。所以我通过更改代码解决了这个问题
mProgressBar.setVisibility(View.GONE);
to
到
runOnUiThread(new Runnable() {
@Override
public void run() {
mProgressBar.setVisibility(View.GONE);
}
});
回答by Christopher Francisco
It's probably due the fact that View.GONE
will prevent the View
from being drawn to the screen, while another View
android:layout_align[POSITION]
component may be defined to that View
, so their position can't be calculated.
这可能是因为这View.GONE
会阻止View
被绘制到屏幕上,而另一个View
android:layout_align[POSITION]
组件可能被定义为View
,因此无法计算它们的位置。
View.INVISIBLE
will work cause it just make it invisible, but the View
is still there and other View
can calculate their position if the align is set to it
View.INVISIBLE
会起作用,因为它只是使它不可见,但是如果对齐设置为它,它View
仍然存在,其他人View
可以计算它们的位置
回答by Александр Крылов
This work for me:
这对我有用:
rootView.findViewById(R.id.progress_bar).setVisibility(View.GONE);
回答by Desmond Hsu
progressBar.setVisibility(ProgressBar.VISIBLE);
progressBar.setVisibility(ProgressBar.VISIBLE);