在Android中围绕编程进度 [英] Centering ProgressBar Programmatically in Android

查看:156
本文介绍了在Android中围绕编程进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这篇关于在Android中围绕编程进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆