6

I made the following layout programmatically:

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

But no mather what I give as values in setMargins, it doesn't have any effect at all.
What is the reason?

The layout has a heigth and width of fill_parent so that can't be the problem..

Thx :)

4

2 回答 2

0
progressLayout.setGravity(Gravity.CENTER); 

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

于 2012-09-05T10:04:53.550 回答
0

试试这个解决方案

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);
于 2012-12-11T11:20:10.733 回答