Có ViewGroup.addView
sao chép LayoutParams
dữ liệu vào bên trong hoặc liên kết với nó? Tôi có thể sử dụng lại cùng một phiên bản LayoutParams
với nhiều cuộc gọi đến addView()
với các chế độ xem khác nhau không?Tôi có thể sử dụng lại LayoutPrams với ViewGroup.addView không?
Không có gì về nó trong apidoc.
WOW
Câu trả lời là NO (kiểm tra bằng thực nghiệm):
public class SymbolPadActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout.LayoutParams labelParams;
/*
* This block to reuse is not working
labelParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
labelParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
labelParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
*/
RelativeLayout mover = new RelativeLayout(this);
TextView textView;
for(int leftMargin = 0; leftMargin<3000; leftMargin += 100) {
for(int topMargin=0; topMargin<800; topMargin += 40) {
// I can't omit these 3 lines
labelParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
labelParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
labelParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
labelParams.leftMargin = leftMargin;
labelParams.topMargin = topMargin;
textView = new TextView(this);
textView.setText("(" + leftMargin + "," + topMargin + ")");
mover.addView(textView, labelParams);
}
}
RelativeLayout.LayoutParams moverParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
moverParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
moverParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
moverParams.leftMargin = 0;
moverParams.topMargin = 0;
RelativeLayout stator = new RelativeLayout(this);
stator.addView(mover, 0, moverParams);
setContentView(stator);
}
}
tôi tái sử dụng params của tôi, nó làm việc! Nhưng cameraview của tôi ngừng hoạt động. :/Tôi thậm chí không thể tái sử dụng một param trong constructor LayoutParms để tạo một O_O khác. –