Android 保证金不起作用

Android 保证金不起作用,android,android-layout,Android,Android Layout,我以编程方式进行了以下布局: LinearLayout progressLayout = new LinearLayout(this); progressLayout.setOrientation(LinearLayout.VERTICAL); TextView t = new TextView(this); t.setText("Test.."); t.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20); Lay

我以编程方式进行了以下布局:

LinearLayout progressLayout = new LinearLayout(this);
    progressLayout.setOrientation(LinearLayout.VERTICAL);

    TextView t = new TextView(this);
    t.setText("Test..");
    t.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);

    LayoutParams l = new LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
    l.setMargins(10, 10, 10, 25);   ===> does not work? 
    t.setLayoutParams(l);

    ProgressBar circle = new ProgressBar(this, null,
            android.R.attr.progressBarStyleLarge);
    circle.setLayoutParams(new LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT));


    progressLayout.setLayoutParams(new LayoutParams(
            android.view.ViewGroup.LayoutParams.FILL_PARENT,
            android.view.ViewGroup.LayoutParams.FILL_PARENT));

    progressLayout.setGravity(Gravity.CENTER);

    progressLayout.addView(t);
    progressLayout.addView(circle);

    this.setContentView(progressLayout);
但不管我在setMargins中给出什么值,它都没有任何效果。
原因是什么

布局具有填充父级的高度和宽度,因此不会出现问题

Thx:)


将文本视图和进度条对齐在中间。

尝试此解决方案

LinearLayout progressLayout = new LinearLayout(this);
        progressLayout.setOrientation(LinearLayout.VERTICAL);

        TextView t = new TextView(this);
        t.setText("Test..");
        t.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);

        LayoutParams l = new LayoutParams(
                android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
                android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
        // ===> does not work?
        // l.setMargins(50, 50, 50, 25);
        // t.setLayoutParams(l);
        l.leftMargin = 10;
        l.topMargin = 0;
        l.rightMargin = 0;
        l.bottomMargin = 150;

        ProgressBar circle = new ProgressBar(this, null,
                android.R.attr.progressBarStyleLarge);
        LayoutParams p = new LayoutParams(
                android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
                android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
        // circle.setLayoutParams(p);
        p.leftMargin = 0;
        p.topMargin = 0;
        p.rightMargin = 0;
        p.bottomMargin = 20;

        progressLayout.setLayoutParams(new LayoutParams(
                android.view.ViewGroup.LayoutParams.FILL_PARENT,
                android.view.ViewGroup.LayoutParams.FILL_PARENT));

        progressLayout.setGravity(Gravity.CENTER);

        progressLayout.addView(t, l);
        progressLayout.addView(circle, p);

        this.setContentView(progressLayout);

您使用的是哪种LayoutParams?您应该使用LinearLayout.LayoutParams,因为您将视图置于LinearLayout中(虽然这不应该是问题,因为两者都是MarginLayoutParams),这正是我想要实现的,但我想在textview和progressbar之间留出一些空白。
LinearLayout progressLayout = new LinearLayout(this);
        progressLayout.setOrientation(LinearLayout.VERTICAL);

        TextView t = new TextView(this);
        t.setText("Test..");
        t.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);

        LayoutParams l = new LayoutParams(
                android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
                android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
        // ===> does not work?
        // l.setMargins(50, 50, 50, 25);
        // t.setLayoutParams(l);
        l.leftMargin = 10;
        l.topMargin = 0;
        l.rightMargin = 0;
        l.bottomMargin = 150;

        ProgressBar circle = new ProgressBar(this, null,
                android.R.attr.progressBarStyleLarge);
        LayoutParams p = new LayoutParams(
                android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
                android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
        // circle.setLayoutParams(p);
        p.leftMargin = 0;
        p.topMargin = 0;
        p.rightMargin = 0;
        p.bottomMargin = 20;

        progressLayout.setLayoutParams(new LayoutParams(
                android.view.ViewGroup.LayoutParams.FILL_PARENT,
                android.view.ViewGroup.LayoutParams.FILL_PARENT));

        progressLayout.setGravity(Gravity.CENTER);

        progressLayout.addView(t, l);
        progressLayout.addView(circle, p);

        this.setContentView(progressLayout);