2012-11-11 25 views
2

我創建了一個TextView一個的LinearLayout內編程:的Android TextView的跨度全寬編程

LinearLayout filmTitleContainer = new LinearLayout(ctx); 
TextView filmName = new TextView(ctx); 
filmName.setBackgroundColor(Color.rgb(Color.YELLOW)); 
filmName.setText(this.screeningTime+" "+this.name); 

文本視圖收縮:

enter image description here

如何將文本設置爲佔據屏幕的整個寬度?當我使用XML佈局文件時,我會設置android:layout_width="match_parent"。什麼是等效的Java代碼?


+0

請介意你的標籤選擇。我不知道你是如何將「android」拼成「adnrdoi」的...... – Charles

回答

9

使用LayoutParams兩個視圖及其容器:

LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 1f); 
filmTitleContainer.setLayoutParams(params); 
filmName.setLayoutParams(params); 

enter image description here