我正在开发Android游戏。我正试图使用边距将图像视图从屏幕的左上角定位,但它不起作用。
主要活动:
@Override
protected void onCreate(Bundle savedInstanceState) {
...
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getRealSize(size);
RelativeLayout relativeLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT
);
relativeLayout.setLayoutParams(layoutParams);
setContentView(relativeLayout);
gameView = new GameView(this, this, size.x, size.y);
relativeLayout.addView(gameView);
...
我的GameBoard课程:
public GameBoard (Activity activity, ...) {
...
revealItemImageView = new ImageView(activity);
revealItemImageView.setImageBitmap(bitmap);
RelativeLayout.LayoutParams revealItemLayoutParams = new RelativeLayout.LayoutParams(
(int)(screenHeight * 0.66), (int)(screenHeight * 0.66));
revealItemLayoutParams.setMargins(500, 500, 0, 0);
revealItemImageView.setLayoutParams(revealItemLayoutParams);
activity.addContentView(revealItemImageView, revealItemLayoutParams);
revealItemImageView.requestLayout();
但是图像显示在左上角,顶部或左侧之间没有空格。为什么呢?
答案 0 :(得分:0)
需要将视图添加到相对布局中,并且需要将相对布局添加为第二个内容视图。
我已经在自己的项目中整理了代码演示。两个图像视图都具有适当的边距并略有重叠。
以下是完整的代码。
package com.zzzzzzzz.testrelativelayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// layer 1
RelativeLayout mainActivityContentViewRelativeLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams mainActivityContentViewLayoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT
);
mainActivityContentViewRelativeLayout.setLayoutParams(mainActivityContentViewLayoutParams);
setContentView(mainActivityContentViewRelativeLayout);
RelativeLayout.LayoutParams backgroundLayoutParams = new RelativeLayout.LayoutParams(100, 100);
backgroundLayoutParams.setMargins(100, 100, 0, 0);
ImageView backgroundView = new ImageView(this);
backgroundView.setImageResource(R.drawable.item_baby_bottle_b);
backgroundView.setLayoutParams(backgroundLayoutParams);
mainActivityContentViewRelativeLayout.addView(backgroundView);
// layer 2
RelativeLayout secondContentViewRelativeLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams secondContentViewLayoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT
);
secondContentViewRelativeLayout.setLayoutParams(secondContentViewLayoutParams);
addContentView(secondContentViewRelativeLayout, secondContentViewLayoutParams);
RelativeLayout.LayoutParams itemLayoutParams = new RelativeLayout.LayoutParams(100, 100);
itemLayoutParams.setMargins(150, 150, 0, 0);
ImageView itemImageView = new ImageView(this);
itemImageView.setImageResource(R.drawable.item_diamond_ring_a);
itemImageView.setLayoutParams(itemLayoutParams);
secondContentViewRelativeLayout.addView(itemImageView);
}
}