在Android中围绕编程进度进度、Android

2023-09-05 08:46:53 作者:志在飞翔

我试图居中进度编程方式使用以下内容:

I'm trying to center a ProgressBar programmatically using the following:

ViewGroup layout = (ViewGroup) findViewById(android.R.id.content).getRootView();
progressBar = newProgressBar(SignInActivity.this,null,android.R.attr.progressBarStyleLarge);
progressBar.setIndeterminate(true);
progressBar.setVisibility(View.VISIBLE);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(100,100);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
layout.addView(progressBar,params);

尺寸设置似乎工作正常,但进度不会在现有布局(通过XML具有相对布局定义)中心。是不是有什么明显错在这里?

The size setting seems to work okay, but the ProgressBar doesn't center in the existing layout (defined by xml with a relative layout). Is there something obviously wrong here?

中的XML如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".test"
android:typeface="monospace">

</RelativeLayout>

即。它只是一个空的相对布局来测试,看看我能得到它以编程方式添加进度条。

i.e. it's just an empty relative layout to test with and see if I can get it to programmatically add a progress bar.

感谢。

推荐答案

如果你想以编程方式做到这一点,你可以像如下:

If you want to do it programatically you can do it like below:

RelativeLayout layout = new RelativeLayout(this);
progressBar = new ProgressBar(SignInActivity.this,null,android.R.attr.progressBarStyleLarge);
progressBar.setIndeterminate(true);
progressBar.setVisibility(View.VISIBLE);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(100,100);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
layout.addView(progressBar,params);

setContentView(layout);