我试图在Google地图视图上显示ProgressBar。如果我在布局编辑器中将其设置为VISIBLE
,则会显示进度条,但这不是我想要的。相反,我希望它为INVISIBLE
,并在后台加载内容时使其可见。但是,进度条保持隐藏状态。
布局如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<!-- Google Maps view -->
<view
android:layout_width="fill_parent"
android:layout_height="fill_parent"
class="com.google.android.gms.maps.MapView"
android:id="@+id/mapView"
android:background="#ddd"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:visibility="visible"/>
<!-- The progress bar to show when stuff is loading in background -->
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/pbLoading"
android:indeterminate="true"
android:progress="0"
android:visibility="invisible"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
与问题相关的代码是这样的:
ProgressBar m_pbLoading;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_map, container, false);
m_pbLoading = (ProgressBar)view.findViewById(R.id.pbLoading);
...
}
// This gets called when stuff starts loading in the background
@Override
public void onStartLoading()
{
Log.i(TAG, "onStartLoading()");
m_pbLoading.setVisibility(View.VISIBLE);
...
}
// This gets called when stuff stops loading in the background
@Override
public void onStopLoading()
{
Log.i(TAG, "onStopLoading()");
m_pbLoading.setVisibility(View.INVISIBLE);
...
}
基本上,进度条最初设置为INVISIBLE
,加载开始时,它应该显示出来。当加载停止时,它将再次被隐藏。但是,这不会发生。只有在布局中将其设置为可见时,才会显示进度条。
答案 0 :(得分:0)
您是否尝试在单独的UI线程中添加setVisibility。
public void run() {
runOnUiThread(new Runnable() {
public void run()
{
//Setvisibility method
}
});
}
答案 1 :(得分:0)
试试
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<!-- The progress bar to show when stuff is loading in background -->
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/pbLoading"
android:indeterminate="true"
android:progress="0"
android:visibility="gone"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
<!-- Google Maps view -->
<view
android:layout_width="fill_parent"
android:layout_height="fill_parent"
class="com.google.android.gms.maps.MapView"
android:id="@+id/mapView"
android:background="#ddd"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:visibility="visible"/>
</RelativeLayout>
和
@Override
public void onStartLoading()
{
Log.i(TAG, "onStartLoading()");
m_pbLoading.setVisibility(View.VISIBLE);
...
}
// This gets called when stuff stops loading in the background
@Override
public void onStopLoading()
{
Log.i(TAG, "onStopLoading()");
m_pbLoading.setVisibility(View.GONE);
...
}
添加强>
或者谷歌地图视图:
@Override
public void onStartLoading()
{
Log.i(TAG, "onStartLoading()");
GoogleMaps.setVisibility(View.GONE);
m_pbLoading.setVisibility(View.VISIBLE);
...
}
// This gets called when stuff stops loading in the background
@Override
public void onStopLoading()
{
Log.i(TAG, "onStopLoading()");
m_pbLoading.setVisibility(View.GONE);
GoogleMaps.setVisibility(View.VISIBLE);
...
}