[英]Android ProgressBar disappears
我有一个不确定的Android ProgressBar所以它只是一个旋转圆圈动画。
它开始显示正常,但在我将其父级( overlayLayout
)的可见性设置为已消失或不可见,然后将其设置为稍后可见时,进度条是看不见的?
<?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()调用中包装我的可见性调用。
几点
根据问题的描述方式,您需要显示用于隐藏/显示overlayLayout
的代码段
建议
如果您只关心进度条在隐藏/显示方面的行为方式,则不需要此代码段:
ProgressBar.Enabled = true;
ProgressBar.ForceLayout();
ProgressBar.Invalidate();
ProgressBar.SetProgress(0, true);
ProgressBar.Visibility = ViewStates.Visible;
您只需要控制具有overlayLayout
id的根布局
private RelativeLayout overlayLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_example);
// Instantiate the layout
overlayLayout = findViewById(R.id.overlayLayout);
// Do the logic how you inflate/show the layout
...
// Hide the overlay layout
overlayLayout.setVisibility(View.GONE);
// Show the overlay layout
overlayLayout.setVisibility(View.VISIBLE);
}
决定可见性值,对于这种情况,我建议使用View.GONE
而不是View.INVISIBLE
了解更多:
我根据你的代码写了一个demo。 为了测试它,我添加了一个按钮来控制显示和隐藏布局。 你可以试试这个:
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
// Instantiate the layout
overlayLayout = FindViewById<LinearLayout>(Resource.Id.overlayLayout);
progressBar = FindViewById<ProgressBar>(Resource.Id.overlayProgressBar);
description = FindViewById<TextView>(Resource.Id.textView1);
description.Text = "Waiting for GPS";
description.SetBackgroundColor(Color.Aqua);
progressBar.SetBackgroundColor(Color.Red);
switchBtn = FindViewById<Button>(Resource.Id.switchButton);
switchBtn.Click += SwitchBtn_Click;
overlayLayout.Visibility = Android.Views.ViewStates.Visible;
isShown = true;
}
private void SwitchBtn_Click(object sender, System.EventArgs e)
{
if (isShown)
{
overlayLayout.Visibility = Android.Views.ViewStates.Gone;
isShown = false;
switchBtn.Text = "Show";
}
else {
overlayLayout.Visibility = Android.Views.ViewStates.Visible;
isShown = true;
switchBtn.Text = "Hide";
}
您可以从演示这个
解决方案是添加一个RunOnUIThread
如下所示:
private void OnNewGPS()
{
mainActivity.RunOnUiThread(() =>
{
overlay.Visibility = ViewStates.Gone; });
}
private void NoGPS()
{
mainActivity.RunOnUiThread(() =>
{
overlay.Visibility = ViewStates.Visible;
});
}
其中mainActivity是对运行视图的Activity的引用。
隐藏布局:
findViewById(R.id.overlayLayout).setVisibility(View.GONE);
再次显示:
findViewById(R.id.overlayLayout).setVisibility(View.VISIBLE);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.