2011-01-05 20 views
6

Tôi có LinearLayout bên trong TableRow. Các LinearLayout được khởi xướng trong các mã, theo cách này:match_parent không điền thông tin gốc!

LinearLayout mainRowLayout = new LinearLayout(this); 
mainRowLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); 
TableRow tr = new TableRow(this); 
tr.addView(mainRowLayout); 

Vấn đề là các LinearLayout không điền vào phụ huynh (đó là TableRow). Các hình ảnh đính kèm minh họa sự cố, như được hiển thị trong hirarchyViewer của Android (Các hình chữ nhật màu xanh lá cây là các điểm của tôi).

"LinearLayout image"

Cảm ơn.

Trả lời

13

Khi tạo bố cục theo chương trình, bạn cần cung cấp cho bố mẹ vùng chứa hướng dẫn bố cục cho trẻ.

// child element 
LinearLayout mainRowLayout = new LinearLayout(this); 

// parent element 
TableRow tr = new TableRow(this); 

// parent element's instructions (layout params for main row) 
TableRow.LayoutParams lopForMainRow = new TableRow.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); 

tr.addView(mainRowLayout, lopForMainRow); 

Tặng thử:]

Các vấn đề liên quan