2011-12-20 20 views
5

Tôi đang cố gắng để tạo ra một FrameLayout lập trình như sau (điểm này sẽ nổi trên trung tâm đáy của một LinearLayout):FrameLayout layout_weight lập trình

FrameLayout bottomFrameLayout = new FrameLayout(context); 
    bottomFrameLayout.setLayoutParams(new FrameLayout.LayoutParams(0,LayoutParams.FILL_PARENT,Gravity.CENTER | Gravity.BOTTOM)); 

Kể từ khi tôi đặt layout_width=0dp, tôi muốn cũng phải thiết lập layout_weight để kiểm soát chiều rộng. Làm thế nào tôi có thể làm điều đó?

Trả lời

16

Bạn cần sử dụng LinearLayout.LayoutParams (int width, int height, float weight) hàm tạo vì FrameLayout là con của LinearLayout do đó bạn đang đặt thông số cho con trong LinearLayout.

Edit:

FrameLayout bottomFrameLayout = new FrameLayout(context); 
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0,LayoutParams.FILL_PARENT,mWeight); 
lp.gravity = Gravity.CENTER | Gravity.BOTTOM; 
bottomFrameLayout.setLayoutParams(lp); 
+0

nếu tôi sử dụng LinearLayout.LayoutParams để thiết lập cân làm thế nào để thiết lập mức độ nghiêm trọng? – prostock

+0

Thử gán giá trị cho trường 'gravity', như' layoutParamsObject.gravity = Gravity.SOME_OF_THE_CONSTANTS' –

+3

@NikolaDespotoski FrameLayout không phải là con của LinearLayout theo tham chiếu, xem http://developer.android.com/reference/android/ widget/FrameLayout.html và FrameLayout.LayoutParams không phải là con của LinearLayout.LayoutParams xem http://developer.android.com/reference/android/widget/FrameLayout.LayoutParams.html. Câu trả lời của bạn đã lỗi thời chưa? – soriak

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