2012-09-05 93 views
6

我編程做了如下佈局:邊距不工作

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,它不會有任何效果可言。
是什麼原因?

佈局具有heigth和FILL_PARENT的寬度,使得不能成爲問題..

THX :)

+0

您使用的是什麼樣的LayoutParams的? – njzk2

+0

RelativeLayout.LayoutParams –

+0

您應該使用LinearLayout.LayoutParams,因爲您將視圖放在LinearLayout中(儘管這不應該是問題,因爲兩者都是MarginLayoutParams) – njzk2

回答

0
progressLayout.setGravity(Gravity.CENTER); 

在對準中心的TextView和進度。

+0

這就是我想要實現的,但我想要一些文本視圖和進度條之間的邊距。 –

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