16

我正在尝试ProgressBar使用以下内容以编程方式居中:

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

大小设置似乎工作正常,但ProgressBar不在现有布局的中心(由具有相对布局的 xml 定义)。这里有什么明显的错误吗?

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>

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

谢谢。

4

4 回答 4

36

我根据@DharmarajRupakheti 的回答写了一门课:

public class ProgressBarHandler {
    private ProgressBar mProgressBar;
    private Context mContext;

    public ProgressBarHandler(Context context) {
        mContext = context;

        ViewGroup layout = (ViewGroup) ((Activity) context).findViewById(android.R.id.content).getRootView();

        mProgressBar = new ProgressBar(context, null, android.R.attr.progressBarStyleLarge);
        mProgressBar.setIndeterminate(true);

        RelativeLayout.LayoutParams params = new
                RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);

        RelativeLayout rl = new RelativeLayout(context);

        rl.setGravity(Gravity.CENTER);
        rl.addView(mProgressBar);

        layout.addView(rl, params);

        hide();
    }

    public void show() {
        mProgressBar.setVisibility(View.VISIBLE);
    }

    public void hide() {
        mProgressBar.setVisibility(View.INVISIBLE);
    }
}

用法:

mProgressBarHandler = new ProgressBarHandler(this); // In onCreate
mProgressBarHandler.show(); // To show the progress bar
mProgressBarHandler.hide(); // To hide the progress bar
于 2014-11-28T13:28:43.567 回答
31

如果您想以编程方式执行此操作,您可以执行以下操作:

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);
于 2013-08-18T18:20:31.607 回答
10

以这种风格使用您的编码,这将提供您想要的结果。

ViewGroup layout = (ViewGroup) findViewById(android.R.id.content).getRootView();

progressBar = new ProgressBar(thisActivity,null,android.R.attr.progressBarStyleLarge);
progressBar.setIndeterminate(true);
progressBar.setVisibility(View.VISIBLE);

RelativeLayout.LayoutParams params = new 
        RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.MATCH_PARENT);

RelativeLayout rl = new RelativeLayout(thisActivity);

rl.setGravity(Gravity.CENTER);
rl.addView(progressBar);

layout.addView(rl,params);
于 2013-10-23T06:34:32.273 回答
-1

您不需要 XML。您可以通过代码全部创建视图:

@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayout layout = new LinearLayout(this);
        ProgressBar pb = new ProgressBar(this, null,
                                         android.R.attr.progressBarStyleLarge);
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        pb.setLayoutParams(params);
        layout.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
        layout.addView(pb);
        setContentView(layout);
}
于 2014-04-04T01:23:05.020 回答