我希望在我的画布下方实现一个 xml 布局,我的画布是一个方形块,其大小取决于手机的宽度(检测宽度并匹配高度)。
我的应用程序始终以纵向模式显示,因此这会在我的画布下方留下一个空隙。由于所有不同的屏幕尺寸,画布下的这个空间不断变化。在本节中,我想添加一个布局,允许我添加文本视图和几个图像按钮。我目前正在 onDraw 中执行所有这些操作,但是这是非常混乱的代码,这意味着由于屏幕之间的巨大尺寸差异,我有 3 种代码变体。
我目前有一个 Game 类,它又调用包含所有 onDraw 代码的 GameView,问题是如何添加布局来填充画布下留下的任何空间?请参阅下面的 Game/GameView 类的基本分类:
游戏类
public class Game extends Activity
{
@Override
public void onCreate(Bundle bundle)
{
super.onCreate(bundle);
setContentView(R.layout.test);
Intent intent = getIntent();
Bundle extras = intent.getExtras();
this.maze = (Maze)getLastNonConfigurationInstance();
if(this.maze == null)
{
this.maze = (Maze)extras.get("maze");
}
// This is where I am setting the view, this leads to my onDraw code which progromatically creates as maze.
// This is a square box taking the width of the screen as the dimensions for the square.
GameView view = (GameView)findViewById(R.id.gameview);
view.setMaze(this.maze);
setContentView(view);
}
}
GAMEVIEW CLASS - 在画布下带有一小段手动创建的文本内容
public class GameView extends View
{
// This is the second canvas within the maze, which sites below the main game maze which is the square i mentioned previously.
// This is where i need to add a layout which dynamically fills any remaining space.
protected void onDraw(Canvas canvas)
{
// Setting the canvas size for previous calcuated values.
canvas.drawRect(0, 0, width, height, background);
if((PhoneWidth < PhoneHeight) && (PhoneWidth < 600))
{
canvas.drawBitmap(arrows, QTRWidth * 2, height + 50, null);
if(CharacterCount >= 3)
{
canvas.drawText(Characters.get(2).Name(), 10, height + 135, GrayTextMedium);
// HIT POINTS
if((float)Characters.get(2).HitPoints() / (float)Characters.get(2).HitPointsMax() < 0.11)
canvas.drawText("HP: " + Characters.get(2).HitPoints(), 10, height + 160, RedTextMedium);
else
canvas.drawText("HP: " + Characters.get(2).HitPoints(), 10, height + 160, GrayTextMedium);
// MAGIC POINTS
if((float)Characters.get(0).MagicPoints() / (float)Characters.get(0).MagicPointsMax() < 0.11)
canvas.drawText("MP: " + Characters.get(2).MagicPoints(), QTRWidth, height + 160, RedTextMedium);
else
canvas.drawText("MP: " + Characters.get(2).MagicPoints(), QTRWidth, height + 160, GrayTextMedium);
}
if(CharacterCount == 1) { canvas.drawText("MONEY: " + Characters.get(0).Money(), 10, height + 80, GrayTextMedium); }
if(CharacterCount == 2) { canvas.drawText("MONEY: " + Characters.get(0).Money(), 10, height + 135, GrayTextMedium); }
if(CharacterCount == 3) { canvas.drawText("MONEY: " + Characters.get(0).Money(), 10, height + 190, GrayTextMedium); }
}
}
}
有人可以帮我清理这段代码,或者把我指向其他地方的详细教程。我一直在研究这个并尝试了各种无济于事的事情,因此任何帮助将不胜感激。谢谢。