2011-04-07 46 views
2
設置佈局屬性

如何從代碼中設置屬於RelativeLayout下的TextView的屬性。如何從代碼

這裏是我的佈局:

<RelativeLayout 
    android:id="@+id/team_left" 
    android:layout_width="wrap_content" 
    android:layout_height="50dip" 
    android:layout_alignParentLeft="true"> 
    <ImageView 
     android:id="@+id/flag_left"  
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/img"/> 
    <TextView 
     android:id="@+id/country_left" 
     android:textColor="@color/txt_color" 
     android:textStyle="bold" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 
</RelativeLayout> 

如何設置,如屬性 「機器人:layout_toRightOf =」 @ ID/flag_left」從代碼中的TextView

+0

你如何訪問該佈局?它是您設置爲內容視圖的佈局還是使用充氣器充氣? – 2011-04-07 12:02:59

+0

佈局設置在創建活動上。我在設置佈局作爲內容視圖 – 2011-04-07 12:12:41

回答

15

您需要使用RelativeLayout.LayoutParams爲TextView的,然後使用addRule

例:

RelativeLayout.LayoutParams params = 
    new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, 
            LayoutParams.FILL_PARENT); 
params.addRule(RelativeLayout.LAYOUT_ABOVE, R.id.flag_left); 
tv.setLayoutParams(params); 
+0

我已經通過這個。你能舉個例子嗎? – 2011-04-07 12:16:58

+0

'RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT); params.addRule(android.R.attr.layout_above,R.id.flag_left); tv.setLayoutParams(params);' – pankajagarwal 2011-04-07 12:26:04

+0

謝謝。我會試試這個。 – 2011-04-07 12:40:31