Android ProgressBar消失了

时间:2019-01-21 02:02:17

标签: c# android xamarin android-progressbar

我有一个不确定的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;
}

首次渲染布局时,在首次隐藏之前:
(我在不好的时候截图了,但是蓝色的绘图显示了圆圈在其加载动画周围移动的位置) enter image description here

隐藏并再次显示后,可以看到progressBar加载视图,但是不再有加载圈:

enter image description here

我开始认为这可能是我的android模拟器出现的问题?不,在我的物理电话上测试时,同样的问题。文本视图仍然显示良好,只是进度条不显示?

解决方案 我没有完全理解它,因为除了progressBar以外,其他所有功能似乎都可以正常工作,但是解决方案来自将可见性调用包装在RunOnUIThread()调用中。

4 个答案:

答案 0 :(得分:2)

几分

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

建议

如果您只关心进度条在隐藏/显示方面的表现,则无需使用此代码段:

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

了解更多信息:

答案 1 :(得分: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下载演示文件

答案 2 :(得分:1)

解决方案是像这样添加RunOnUIThread

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

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

mainActivity是对运行视图的活动的引用。

答案 3 :(得分:0)

隐藏布局:

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

再次显示:

findViewById(R.id.overlayLayout).setVisibility(View.VISIBLE);
相关问题