【问题标题】:ProgressBar is not shown when visibility is set to VISIBLE当可见性设置为 VISIBLE 时,ProgressBar 不显示
【发布时间】:2015-08-05 15:04:14
【问题描述】:

我正在尝试在 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,当加载开始时,它应该会显示出来。当加载停止时,它将再次被隐藏。但是,这不会发生。进度条只有在我将其设置为在布局中可见时才可见。

【问题讨论】:

  • 确保这两个方法 onStartLoading() & onStopLoading() 在 ui 线程上运行
  • @KaranMer 他们都在 UI 线程上运行。
  • 尝试在 oncreate 中让它可见一次并判断它是否显示
  • @KaranMer 它是这样显示的。
  • 那么我猜你的方法不在 ui 线程上,甚至没有被调用。

标签: android


【解决方案1】:

您是否尝试在单独的 UI 线程中添加 setVisibility。

 public void run() {  
    runOnUiThread(new Runnable() { 
        public void run() 
        { 
          //Setvisibility method
        } 
    }); 
}

【讨论】:

    【解决方案2】:

    试试看

    <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);
        ...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-31
      • 2021-12-03
      • 1970-01-01
      • 1970-01-01
      • 2014-01-11
      • 2011-05-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多