我想在我的画布下面实现一个xml布局,我的画布是一个方形块,它的大小取决于手机的宽度(检测宽度并匹配高度)。
我的应用程序总是以人像模式显示,因此这在我的画布下面留下了一个空白。由于屏幕大小不同,画布下的空间不断变化。在本节中,我想添加一个布局,允许我添加文本视图和几个图像按钮。我目前正在onDraw中完成这一切,但这是一个非常混乱的代码,这意味着由于屏幕之间的巨大尺寸差异,我有3种代码变体。
我目前有一个Game类,它反过来调用由所有onDraw代码组成的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); }
}
}
}
有人能帮我清理一下这个代码吗,或者在其他地方给我指一个详细的教程。我一直在调查这件事,并尝试了各种各样的事情,但都无济于事,因此任何帮助都将不胜感激。谢谢
我希望在画布下面实现一个xml布局,我的画布是方形方块,大小取决于手机的宽度(检测宽度并匹配高度)。
然后将GameView
包装在另一个布局中,并在其下方放置您想要的任何内容(在Game
活动中使用此布局):
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent">
<your.GameView android:layout_width="match_parent" android:layout_height="wrap_content"/>
<RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"/>
</LinearLayout>
假设当前的GameView
正确地覆盖了onMeasure()
方法,使其本身成为一个正方形,那么RelativeLayout
将获得所有剩余空间,您可以在其中放置额外的视图。
由于屏幕大小不同,画布下的空间不断变化。在本节中,我想添加一个布局,允许我添加文本视图和几个图像按钮。
你是否计划根据可用空间添加额外的视图(例如,如果高度大于x,那么在一些按钮上也添加TextView
或类似的东西?)?如果是,则为该间隙制作自己的布局,并在该自定义布局的onMeasure()
方法中查看可用空间,并仅使用适合该空间的视图。