如果容器布局在滚动视图中,则Android中心重力不起作用

Jag*_*uar -18 android scrollview android-layout layout-gravity android-gravity

我正在创建一个ProgressBar放置在LinearLayout的垂直中心(其他布局不起作用;我需要使用LinearLayout).这是一些代码:

LinearLayout linearLayout = new LinearLayout(context);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.MATCH_PARENT,
        LinearLayout.LayoutParams.MATCH_PARENT);
linearLayout.setLayoutParams(layoutParams);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setGravity(Gravity.CENTER);

//add progressbar
ProgressBar progressBar = new ProgressBar(context);
progressBar.setLayoutParams(new ViewGroup.LayoutParams(
        ViewGroup.LayoutParams.WRAP_CONTENT,
        ViewGroup.LayoutParams.WRAP_CONTENT));
linearLayout.addView(progressBar);
Run Code Online (Sandbox Code Playgroud)

我有一个ContainerView,它将保存上面创建的LinearLayout.像这样的东西:

public class ContainerView extends ScrollView {
    public ContainerView(Context context) {
        super(context);
        initView(context);
    }

    private void initView(Context context){
        addView(linearLayout);     //which is created in the snippet above
    }
} 
Run Code Online (Sandbox Code Playgroud)

如果我使用LinearLayout扩展ContainerView,一切正常.

如果我使用ScrollView扩展ContainerView,则ProgressBar水平居中但不垂直居中.

这种双重行为背后的原因是什么?我的目标是在下载一些数据时显示ProgressBar; 下载数据后,我需要在LinearLayout中显示它.

(注意:使用ScrollView的原因是下载的数据可能太大而不适合小屏幕.这可能看起来像ListView的用例,但还有一些其他原因阻止我使用ListView.)

Jag*_*uar 8

在将以下属性设置为我的父ScrollView后,我得到了它的工作:

android:fillViewport="true"
Run Code Online (Sandbox Code Playgroud)