安卓进度条消失了

3

我有一个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;
}

当布局首次呈现时,在第一次隐藏之前:
(我截图的时间不好,但蓝色的绘画显示了圆圈在其加载动画中移动的位置) enter image description here

在它被隐藏并再次显示后,progressBar加载视图仍然存在,但不再有加载圆圈:

enter image description here

我开始认为这可能只是我的Android模拟器的问题? 不是的,在我的物理手机上测试时也出现了同样的问题。文本视图仍然正常显示,只是进度条没有显示?

解决方案 我并不完全理解它,因为除了progressBar之外,其他所有东西似乎都能正常工作,但是一个解决方案来自于在RunOnUIThread()调用中包装我的可见性调用。


1
你用的是什么代码来显示/隐藏父视图“overlayLayout”?你能否在原帖中更新上述内容? - pinedax
是的,它应该是这样的: ProgressBar.Visibility = View.VISIBLE; 或者 ProgressBar.Visibility = View.GONE; - unzila
我建议您发布更多代码细节,这样我们就可以为您进行测试。 - AbbyWang - MSFT
你解决了这个问题吗? - AbbyWang - MSFT
@AbbyWang 是的,谢谢你!解决方案是为UI调用添加RunOnUiThread()包装器。 - Moffen
@Moffen 好的,抱歉我没有注意到。 - AbbyWang - MSFT
4个回答

2

几点建议

根据问题的描述,您需要展示用于隐藏/显示overlayLayout的代码片段。

建议

如果您只关心进度条在隐藏/显示方面的行为,那么不需要这个片段:

Original Answer翻译成最初的回答

ProgressBar.Enabled = true;
ProgressBar.ForceLayout();
ProgressBar.Invalidate();
ProgressBar.SetProgress(0, true);
ProgressBar.Visibility = ViewStates.Visible;

你只需要控制具有id为overlayLayout的根布局。最初的回答。
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。阅读更多信息请参考以下链接: "最初的回答"

嗨@joshuadeguzman,感谢你迄今为止的帮助!我已经在问题中添加了更多信息。 - Moffen

1

我根据您的代码编写了一个演示。为了测试它,我添加了一个按钮来控制布局的显示和隐藏。 您可以尝试这样做:

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";
            }

您可以从this下载演示文稿。

嗨@AbbyWang,我添加了一些更多的细节,感谢你迄今为止的帮助! - Moffen
我根据你的代码修改了我的演示,它仍然可以正常工作。如果方便的话,你能否分享你的完整项目或演示呢?这样我们就可以找出你的问题所在。 - AbbyWang - MSFT

1
解答:解决方法是添加一个 RunOnUIThread,如下所示:

private void OnNewGPS()
{
    mainActivity.RunOnUiThread(() =>
    {
        overlay.Visibility = ViewStates.Gone;     });
}

private void NoGPS()
{
    mainActivity.RunOnUiThread(() =>
    {
        overlay.Visibility = ViewStates.Visible;
    });
}

其中mainActivity是对活动的引用,视图正在其中运行。


0

隐藏布局:

findViewById(R.id.overlayLayout).setVisibility(View.GONE);

再次显示它:

findViewById(R.id.overlayLayout).setVisibility(View.VISIBLE);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接