我有一个Android ProgressBar,它是不确定的,因此只是一个旋转的圆形动画。
一开始显示正常,但是在我将其父视图(overlayLayout
)的可见性设置为gone或invisible,然后稍后将其设置回visible之后,进度条就看不见了?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/overlayLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:gravity="center" >
<TextView
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="match_parent"
android:textAlignment="center"
android:layout_margin="10dp"
android:layout_height="wrap_content"
android:id="@+id/overlayLabelText" />
<ProgressBar
android:id="@+id/overlayProgressBar"
android:layout_below="@id/overlayLabelText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:foregroundGravity="center"
android:indeterminate="true" />
</RelativeLayout>
编辑: 我不确定视图是否被包含,但进度条只是没有渲染或者说ProgressBar视图本身被完全排除了,因为我无法访问UI视图层次结构。
到目前为止,我已经尝试过:
ProgressBar.Enabled = true;
ProgressBar.ForceLayout();
ProgressBar.Invalidate();
ProgressBar.SetProgress(0, true);
ProgressBar.Visibility = ViewStates.Visible;
但是目前还没有突破。
编辑2:感谢大家的帮助。我已经转而通过编程创建布局——这是我的完整代码:
overlay = new RelativeLayout(mainActivity)
{
LayoutParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent)
};
overlay.SetBackgroundColor(Color.WhiteSmoke);
overlay.SetGravity(GravityFlags.Center);
description = new TextView(mainActivity)
{
LayoutParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.WrapContent),
Gravity = GravityFlags.Center,
TextSize = 18,
Id = 1523112
};
description.Text = "Waiting for GPS";
description.SetBackgroundColor(Color.Aqua);
progressBar = new ProgressBar(mainActivity)
{
Indeterminate = true,
};
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.WrapContent);
lp.AddRule(LayoutRules.Below, description.Id);
progressBar.LayoutParameters = lp;
progressBar.SetBackgroundColor(Color.Red);
container.AddView(overlay);
overlay.AddView(description);
overlay.AddView(progressBar);
通过两种隐藏和显示方法:
private void OnGpsUpdate()
{
overlay.Visibility = ViewStates.Gone;
}
private void NoGPS()
{
description.Text = "Waiting for GPS";
overlay.Visibility = ViewStates.Visible;
}
当布局首次呈现时,在第一次隐藏之前:
(我截图的时间不好,但蓝色的绘画显示了圆圈在其加载动画中移动的位置)
在它被隐藏并再次显示后,progressBar加载视图仍然存在,但不再有加载圆圈:
我开始认为这可能只是我的Android模拟器的问题? 不是的,在我的物理手机上测试时也出现了同样的问题。文本视图仍然正常显示,只是进度条没有显示?
解决方案 我并不完全理解它,因为除了progressBar之外,其他所有东西似乎都能正常工作,但是一个解决方案来自于在RunOnUIThread()调用中包装我的可见性调用。