我一直在尝试制作一个简单的弹出窗口,以便在用户点击精灵时显示。我为弹出窗口制作了一个布局,它没有显示出来。我错过了什么吗?
编辑:这是我的Activity类的其余部分:
int.__dict__['__add__'].__class__.
<class 'method-wrapper'>
我的布局:
public class UniverseActivity extends AppCompatActivity {
public static final String TAG = UniverseActivity.class.getSimpleName();
private UniverseThread thread;
private Galaxy player;
public Galaxy getPlayer() {
return player;
}
//components:
private SeekBar speedBar;
private ProgressBar timeBar;
private UniverseView surfaceView;
private ImageView playerSprite;
private ImageView otherSprite;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_universe);
speedBar = (SeekBar) findViewById(R.id.seekBar);
timeBar = (ProgressBar) findViewById(R.id.progressBar);
surfaceView = (UniverseView) findViewById(R.id.surfaceView);
playerSprite = (ImageView) findViewById(R.id.playerGalaxy);
player = new Galaxy("Milky Way", true, new Velocity(), playerSprite, 1);
otherSprite = (ImageView) findViewById(R.id.otherGalaxy);
playerSprite.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PopupWindow infoWindow = new PopupWindow(findViewById(R.id.galaxy_info_popup));
Log.d(TAG, "Player sprite clicked, stopping time and bringing up window...");
Log.d(TAG, "Will this please work?");
speedBar.setProgress(0);
infoWindow.showAtLocation(surfaceView, Gravity.BOTTOM, 10, 10);
infoWindow.update(50,50,300,500);
}
});
}
答案 0 :(得分:1)
最后一行应该是 -
infoWindow.showAtLocation(surfaceView, Gravity.BOTTOM, 10, 10);
infoWindow.update(50, 50, 300, 80);
希望它对您有用:)
答案 1 :(得分:0)
使用构造函数PopupWindow(View view)
不会设置实际的弹出窗口大小。默认为0高度和0宽度。在创建弹出窗口或使用其他构造函数后,您可能需要调用setWidth()
和setHeight()
。 Here是PopupWindows上的Android文档。我可能会使用PopupWindow(View view, int width, int height)
。另一个问题可能是您传入的视图可能无法正确膨胀或根据情况可能无效。另一种解决方案是调用update()
并指定所需的大小。
答案 2 :(得分:0)
我无法让PopUpWindow
工作,但使用Android Dialog
课程,我的效果更轻松。
这就是我最终的结果:
infoPopUp = new Dialog(this);
infoPopUp.setContentView(R.layout.info_popup);
在onClickListener中:
Log.d(TAG, "Player sprite clicked, stopping time and bringing up window...");
speedBar.setProgress(0);
infoPopUp.setTitle(player.getName());
((ImageView)infoPopUp.findViewById(R.id.infoPicture)).setImageDrawable(player.getSprite().getDrawable());
infoPopUp.findViewById(R.id.option1).setVisibility(View.INVISIBLE);
infoPopUp.findViewById(R.id.option2).setVisibility(View.INVISIBLE);
infoPopUp.findViewById(R.id.continueButton).setVisibility(View.VISIBLE);
((TextView)infoPopUp.findViewById(R.id.infoText)).setText(player.toString());
infoPopUp.show();