Android RelativeLayout LayoutParams setMargins(500500,0,0)无效
我正在开发一款Android游戏。我正在尝试使用边距将图像视图放置在远离屏幕左上角的位置,但它不起作用 主要活动:Android RelativeLayout LayoutParams setMargins(500500,0,0)无效,android,imageview,android-relativelayout,Android,Imageview,Android Relativelayout,我正在开发一款Android游戏。我正在尝试使用边距将图像视图放置在远离屏幕左上角的位置,但它不起作用 主要活动: @Override protected void onCreate(Bundle savedInstanceState) { ... Display display = getWindowManager().getDefaultDisplay(); Point size = new Point(); display.getRealSize(size)
@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);
...
和我的游戏板类:
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();
但图像显示在左上角的右上角,顶部和左侧之间没有空间。为什么?需要将视图添加到相对布局,并且需要将相对布局添加为第二个内容视图 我在它自己的项目中做了一个代码演示。这两个图像视图都具有适当的边距,并且略微重叠 下面是完整的代码
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);
}
}需要将视图添加到相对布局,并且需要将相对布局添加为第二个内容视图 我在它自己的项目中做了一个代码演示。这两个图像视图都具有适当的边距,并且略微重叠 下面是完整的代码
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);
}
}试着将其添加到
相对值yout
而不是感谢您指出这一点。再加上添加第二个内容视图正是我所需要的。我正在创建一个答案,以便有此问题的其他人可以看到完整的代码。请尝试将其添加到相对值yout
中。谢谢您指出这一点。再加上添加第二个内容视图正是我所需要的。我正在创建一个答案,以便其他有此问题的人可以看到完整的代码。