Bạn có thể sử dụng RelativeLayout. Giả sử bạn muốn có một ImageView 30x40 ở vị trí (50,60) bên trong bố cục của bạn. Một nơi nào đó trong hoạt động của bạn:
// Some existing RelativeLayout from your layout xml
RelativeLayout rl = (RelativeLayout) findViewById(R.id.my_relative_layout);
ImageView iv = new ImageView(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(30, 40);
params.leftMargin = 50;
params.topMargin = 60;
rl.addView(iv, params);
Thêm ví dụ:
Places hai 30x40 ImageViews (một màu vàng, một màu đỏ) tại (50,60) và (80,90), tương ứng:
RelativeLayout rl = (RelativeLayout) findViewById(R.id.my_relative_layout);
ImageView iv;
RelativeLayout.LayoutParams params;
iv = new ImageView(this);
iv.setBackgroundColor(Color.YELLOW);
params = new RelativeLayout.LayoutParams(30, 40);
params.leftMargin = 50;
params.topMargin = 60;
rl.addView(iv, params);
iv = new ImageView(this);
iv.setBackgroundColor(Color.RED);
params = new RelativeLayout.LayoutParams(30, 40);
params.leftMargin = 80;
params.topMargin = 90;
rl.addView(iv, params);
Places một 30x40 ImageView vàng tại (50,60) và một ImageView đỏ 30x40 < 80,90>so với các ImageView vàng:
RelativeLayout rl = (RelativeLayout) findViewById(R.id.my_relative_layout);
ImageView iv;
RelativeLayout.LayoutParams params;
int yellow_iv_id = 123; // Some arbitrary ID value.
iv = new ImageView(this);
iv.setId(yellow_iv_id);
iv.setBackgroundColor(Color.YELLOW);
params = new RelativeLayout.LayoutParams(30, 40);
params.leftMargin = 50;
params.topMargin = 60;
rl.addView(iv, params);
iv = new ImageView(this);
iv.setBackgroundColor(Color.RED);
params = new RelativeLayout.LayoutParams(30, 40);
params.leftMargin = 80;
params.topMargin = 90;
// This line defines how params.leftMargin and params.topMargin are interpreted.
// In this case, "<80,90>" means <80,90> to the right of the yellow ImageView.
params.addRule(RelativeLayout.RIGHT_OF, yellow_iv_id);
rl.addView(iv, params);
Nguồn
2010-07-20 22:58:11
cũng thấy 'view.setTranslationX() 'hoặc' view.offsetLeftAndRight() ' –
Tôi vừa mới phát hành một thư viện mà có thể đã được quan tâm ở đây. github.com/ManuelPeinado/ImageLayout – Manuel
Điều này rất khó vì 99,9% thời gian định vị tuyệt đối là một ý tưởng tồi trên Android. Nếu bạn đang viết một ứng dụng sẽ * CHỈ * được chạy trên một thiết bị vật lý, thì điều này có thể hoạt động, nhưng đó thường không phải là một giả định an toàn để thực hiện. Ví dụ: không tải video này lên google play.Nó hoạt động tốt trên iOS vì chỉ có một số ít thiết bị phần cứng và bạn có thể xây dựng bảng phân cảnh tùy chỉnh cho từng thiết bị. – edthethird