java - 因为我无法在动态大小的 Canvas 下方添加 xml 布局

标签 java android xml android-canvas screen-size

我希望在我的 Canvas 下方实现一个 xml 布局,我的 Canvas 是一个方形 block ,其大小取决于手机的宽度(检测宽度并匹配高度)。

我的应用程序始终以纵向模式显示,因此这会在我的 Canvas 下方留下一个空隙。由于各种屏幕尺寸, Canvas 下的这个空间不断变化。在本节中,我想添加一个布局,允许我添加 TextView 和几个图像按钮。我目前在 onDraw 中执行所有这些操作,但是这是非常困惑的代码,这意味着由于屏幕之间的巨大尺寸差异,我有 3 种代码变体。

我目前有一个 Game 类,它依次调用由所有 onDraw 代码组成的 GameView,问题是我如何添加布局来填充 Canvas 下剩余的任何空间?请参阅下面的 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 - 在 Canvas 下有一小段手动创建的文本内容

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); }
        }
    }
}

有人可以帮我清理这段代码,或者给我指点其他地方的详细教程。我一直在研究这个问题并尝试各种方法都无济于事,因此非常感谢您的帮助。谢谢。

最佳答案

I am looking to implement a xml layout below my canvas, my canvas is a square block which baries in size depending on the width of the phone (detects width and matches height).

然后将 GameView 包裹在另一个布局中,并在其下方放置您想要的任何内容(在 Game Activity 中使用此布局):

<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 将获得所有剩余空间,您可以在其中放置额外的 View 。

Due to all the various screen sizes this space under the canvas is constantly changing. Within this section I would like to add a layout, allowing me to add textviews and a couple of image buttons.

您是否计划根据可用空间添加额外的 View (例如,如果高度大于 x,则还要在某些按钮顶部添加 TextView 或类似的东西?)?如果是,则为该间隙制作您自己的布局,并在该自定义布局的 onMeasure() 方法中查看可用空间并仅使用适合该空间的 View 。

关于java - 因为我无法在动态大小的 Canvas 下方添加 xml 布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18190780/

相关文章:

java - 如何覆盖字节数组中的特定 block

java - 确定 TLS 数据包 Java/Android 的数据包大小

android - 如何将动画列表设置为 xml 属性

android - DupelicateEntry,/DragAndDropPermissionCompat.class 问题

xml - Xpath查询以处理XML

java - 如何进入 eclipse 类(class)?

java - 在 Android 中查看动态文本

c# - 从字符串 C# 解析 XML

xml - 如何使用 Scala 将 xml 文档解析为流?

java - spring mvc 中不允许 405 方法,但其他操作和 Controller 工作正常?