简体   繁体   English

Android ProgressBar消失了

[英]Android ProgressBar disappears

I have an android ProgressBar which is indeterminate so it is simply a revolving circle animation. 我有一个不确定的Android ProgressBar所以它只是一个旋转圆圈动画。

It starts off displaying fine, but after I set the visibility of its parent ( overlayLayout ) to either gone or invisible and then set it back to visible later on, the progress bar is unseen? 它开始显示正常,但在我将其父级( 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>

EDIT: I'm unsure if the view is included but the progress bar is just not rendered or whether the ProgressBar view itself is completely excluded as I can't access the UI view hierarchy. 编辑:我不确定是否包含视图但是进度条没有呈现或者ProgressBar视图本身是否被完全排除,因为我无法访问UI视图层次结构。

So far I have tried: 到目前为止,我尝试过:

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

But have had no breakthroughs yet. 但是还没有突破。

EDIT 2: Thankyou everyone for your help so far. 编辑2:谢谢大家到目前为止的帮助。 I have switched to creating the layout programatically - this is my full code: 我已经切换到以编程方式创建布局 - 这是我的完整代码:

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

With the two hiding and showing methods: 用两种隐藏和显示方法:

private void OnGpsUpdate()
{
    overlay.Visibility = ViewStates.Gone;
}

private void NoGPS()
{
    description.Text = "Waiting for GPS";
    overlay.Visibility = ViewStates.Visible;
}

When the layout is first rendered, before its hidden for the first time: 首次渲染布局时,在第一次隐藏布局之前:
(I screenshotted at a bad time, but blue drawing shows where the circle is moving around its loading animation) (我在很糟糕的时候截图,但是蓝色绘图显示了圆圈在其加载动画周围移动的位置) 在此输入图像描述

After its been hidden and shown again, the progressBar loading view is there but there's no loading circle anymore: 在隐藏并再次显示之后,progressBar加载视图就在那里,但是不再有加载圈:

在此输入图像描述

I am starting to think it may just be a problem with my android emulator? 我开始认为它可能只是我的Android模拟器的问题? Nope, same problem when testing on my physical phone. 不,在我的手机上测试时同样的问题。 Text view still shows fine, its just the progress bar doesnt show? 文本视图仍然显示正常,它只是进度条没有显示?

SOLUTION I don't fully understand it, seeing as everything else seemed to work except the progressBar, but a solution came from wrapping my visibility calls in a RunOnUIThread() call. 解决方案我不完全理解它,看到除了progressBar之外其他一切似乎都有效,但解决方案来自于在RunOnUIThread()调用中包装我的可见性调用。

Few points 几点

Based on how the problem was described, there is a need for you to show the code snippet you used for hiding/showing the overlayLayout 根据问题的描述方式,您需要显示用于隐藏/显示overlayLayout的代码段

Recommendations 建议

If you're only concerned with how the progress bar should behave in terms of hiding/showing, there's no need for this snippet: 如果您只关心进度条在隐藏/显示方面的行为方式,则不需要此代码段:

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

You just need to control the root layout which has the id of overlayLayout 您只需要控制具有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);
}

Decide on the visibility value, for this scenario, I'd recommend View.GONE rather than View.INVISIBLE 决定可见性值,对于这种情况,我建议使用View.GONE而不是View.INVISIBLE

Read more on: 了解更多:

I wrote a demo based on your code. 我根据你的代码写了一个demo。 And to test it, I added a button to control the show and hide of the layout. 为了测试它,我添加了一个按钮来控制显示和隐藏布局。 You may try this: 你可以试试这个:

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

You can download the demo from this 您可以从演示这个

The solution was to add a RunOnUIThread like so: 解决方案是添加一个RunOnUIThread如下所示:

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

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

Where mainActivity is a reference to the Activity the views are running in. 其中mainActivity是对运行视图的Activity的引用。

to hide layout: 隐藏布局:

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

to display it again : 再次显示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM