2012-06-26 50 views
0

我對此有點困惑。我有一個應用程序,用戶在其中繪製矩形文本對象。我在xml中的相對佈局中添加了一些textviews(可以說我最多有3個文本對象)。對象可以移動和調整大小。我添加此代碼爲每個TextView的android - LayoutParams如何工作?

TextView tv=(TextView) findViewById(R.id.text1);    
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT); 
System.out.println(texts.get(i).Sx+ " , "+texts.get(i).Sy+ " , "+ texts.get(i).Lx+ " , "+texts.get(i).Ly); 
if(texts.get(i).Sx<=texts.get(i).Lx){ 
    if(texts.get(i).Sy<=texts.get(i).Ly){      
     lp.setMargins((int)texts.get(i).Sx, (int)texts.get(i).Sy, (int)texts.get(i).Lx, (int)texts.get(i).Ly); 
    } else{ 
     lp.setMargins((int)texts.get(i).Sx, (int)texts.get(i).Ly, (int)texts.get(i).Lx, (int)texts.get(i).Sy); 
    } 
} else{ 
    if(texts.get(i).Sy<=texts.get(i).Ly){ 
     lp.setMargins((int)texts.get(i).Lx, (int)texts.get(i).Sy, (int)texts.get(i).Sx, (int)texts.get(i).Ly); 
    } else{ 
     lp.setMargins((int)texts.get(i).Lx, (int)texts.get(i).Ly, (int)texts.get(i).Sx, (int)texts.get(i).Sy); 
    } 
}   
tv.setLayoutParams(lp); 
tv.setWidth((int)(texts.get(i).width)); 
tv.setHeight((int)(texts.get(i).height)); 
tv.setTextSize(texts.get(i).textSize); 
tv.setText(text.text); 
tv.setTextColor(Color.WHITE); 
tv.requestLayout(); 

SX,SY是對象的以像素爲單位和LX,Ly的結束庫斯起始庫斯。

在XML中添加此:

<RelativeLayout 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent" 
    android:id="@+id/texts" > 

    <TextView 
     android:layout_height="fill_parent" 
     android:layout_width="fill_parent" 
     android:id="@+id/text1" /> 

    <TextView 
     android:layout_height="fill_parent" 
     android:layout_width="fill_parent" 
     android:id="@+id/text2" /> 

    <TextView 
     android:layout_height="fill_parent" 
     android:layout_width="fill_parent" 
     android:id="@+id/text3" />    

</RelativeLayout> 

這似乎在將文本放置在正確的位置爲工作。但是textview的寬度和高度似乎並不正確。這是以像素爲單位設置邊距的問題嗎?一些啓發現在將非常有用

+0

如果您@ text1的垂直和水平填充母,再沒有什麼留給其他 – max4ever

+0

它是一個RelativeLayout的,使他們能夠在另一個之上是嗎?問題是,只有text1它也有問題 – george

回答

0

LayoutParams用於動態調整視圖。你可以像下面,上面,底部,頂部,對齊,邊距和所有的(就像你通過xml做的一樣)添加規則,最後你可以設置這個佈局參數到你的視圖。例如:

// Set the params eg. for a button. 

    RelativeLayout.LayoutParams button_params = new RelativeLayout.LayoutParams(RelativeLayout 
     .LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 

    button_params.addRule(RelativeLayout.LEFT_OF, 
          any_view.getId()); 
    button_params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 
          RelativeLayout.TRUE); 
    button_params.bottomMargin = screen_height/15; 
    button_params.rightMargin = 20; 
    button.setLayoutParams(button_params); 
+0

,但是這不是我用lp.setMargin方法做的嗎?我希望我的textView恰好適合用戶繪製的框。我無法預測盒子在哪裏,但我有咕咕聲(Sx,Sy,Lx,Ly)。我也試過 \t \t \t \t lp.topMargin =(int)texts.get(i).Sy; \t \t \t \t lp.bottomMargin =(int)texts.get(i).Ly; \t \t \t \t lp.leftMargin =(int)texts.get(i).Sx; \t \t \t \t lp.rightMargin =(int)texts.get(i).Lx; 但問題仍然存在 – george

0

您首先將width/height設置爲FILL_PARENT,然後用px覆蓋它?

嘗試這樣的事情

View temp = this.findViewById(recent); 
RelativeLayout.LayoutParams relativeParams = (LayoutParams) temp.getLayoutParams(); 

relativeParams.setMargins(0, 0, 0, 50); // llp.setMargins(left, top, right, bottom); 
relativeParams.width = 123; 
relativeParams.height = 123; 
temp.setLayoutParams(relativeParams); 
+0

上面的代碼中的「最近」是什麼意思?我的textView?或根相對佈局? – george

+0

你的textView,基本上任何R.id.something – max4ever

相關問題