如何以编程方式将进度条置于布局中心?

时间:2011-05-18 13:15:33

标签: java android listview progress-bar android-linearlayout

我想以编程方式将代码中的进度条中心化。这是我的代码:

  LinearLayout streamLayout = new LinearLayout(this);
            streamLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
            streamLayout.setOrientation(LinearLayout.VERTICAL);
            streamLayout.setGravity(Gravity.CENTER);

            LinearLayout newsLayout = new LinearLayout(this);
            streamLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
            streamLayout.setOrientation(LinearLayout.VERTICAL);
            streamLayout.setGravity(Gravity.CENTER);

            mSwipeView.addView(streamLayout);
            mSwipeView.addView(newsLayout);
            mSwipeView.addView(new LinearLayout(this));

            ((LinearLayout) mSwipeView.getChildContainer().getChildAt(0)).addView(streamListView);
            ((LinearLayout) mSwipeView.getChildContainer().getChildAt(0)).addView(streamProgressBar);
            ((LinearLayout) mSwipeView.getChildContainer().getChildAt(1)).addView(newsListView);
            ((LinearLayout) mSwipeView.getChildContainer().getChildAt(1)).addView(newsProgressBar);
            ((LinearLayout) mSwipeView.getChildContainer().getChildAt(2)).addView(chartImageView);

如何让进度条垂直居中,水平放置在我的布局中?

2 个答案:

答案 0 :(得分:2)

尝试使用LinearLayout.layoutParams并将它们设置为视图。它们包含一个可以设置为Gravity.CENTER或类似的场重力。

See This Link

答案 1 :(得分:0)

View.setScaleType(ScaleType.CENTER_INSIDE);// or fitCenter

希望有所帮助:)