Android 进度条消失
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 loading view还在,但是没有loading circle了:
我开始认为这可能只是我的 android 模拟器的问题? 不,在我的物理 phone 上测试时出现同样的问题。文本视图仍然显示正常,只是进度条不显示?
解决方案
我不完全理解它,因为除了 progressBar 之外其他一切似乎都有效,但解决方案来自将我的可见性调用包装在 RunOnUIThread() 调用中。
隐藏布局:
findViewById(R.id.overlayLayout).setVisibility(View.GONE);
再次显示:
findViewById(R.id.overlayLayout).setVisibility(View.VISIBLE);
几分
根据问题的描述方式,您需要显示用于 hiding/showing overlayLayout
的代码片段
建议
如果您只关心进度条在 hiding/showing 方面的表现,则不需要此代码段:
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
阅读更多内容:
我根据你的代码写了一个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";
}
您可以从 this
下载演示
解决方案是像这样添加一个 RunOnUIThread
:
private void OnNewGPS()
{
mainActivity.RunOnUiThread(() =>
{
overlay.Visibility = ViewStates.Gone; });
}
private void NoGPS()
{
mainActivity.RunOnUiThread(() =>
{
overlay.Visibility = ViewStates.Visible;
});
}
其中 mainActivity 是对 Activity 视图的引用 运行 in.
我有一个 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 loading view还在,但是没有loading circle了:
我开始认为这可能只是我的 android 模拟器的问题? 不,在我的物理 phone 上测试时出现同样的问题。文本视图仍然显示正常,只是进度条不显示?
解决方案 我不完全理解它,因为除了 progressBar 之外其他一切似乎都有效,但解决方案来自将我的可见性调用包装在 RunOnUIThread() 调用中。
隐藏布局:
findViewById(R.id.overlayLayout).setVisibility(View.GONE);
再次显示:
findViewById(R.id.overlayLayout).setVisibility(View.VISIBLE);
几分
根据问题的描述方式,您需要显示用于 hiding/showing overlayLayout
建议
如果您只关心进度条在 hiding/showing 方面的表现,则不需要此代码段:
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
阅读更多内容:
我根据你的代码写了一个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";
}
您可以从 this
下载演示解决方案是像这样添加一个 RunOnUIThread
:
private void OnNewGPS()
{
mainActivity.RunOnUiThread(() =>
{
overlay.Visibility = ViewStates.Gone; });
}
private void NoGPS()
{
mainActivity.RunOnUiThread(() =>
{
overlay.Visibility = ViewStates.Visible;
});
}
其中 mainActivity 是对 Activity 视图的引用 运行 in.