简体   繁体   English

屏幕中的中心视图内容

[英]Center view contents in screen

I've been porting a Java game to Android, and I now have essentially only one step remaining: the original desktop version created a window and drew in a big black box the same size. 我一直在将Java游戏移植到Android,现在只剩下一个步骤:原始的桌面版本创建了一个窗口,并画了一个大小相同的大黑匣子。 The entire game field is less than the resolution of a standard Android device, but readably so, so I'd like to simply retain all the coordinates as we know them and center playing field in the screen, extending the border in all directions. 整个游戏场都小于标准Android设备的分辨率,但可以理解,因此,我想简单地保留我们所知道的所有坐标并在屏幕上将游戏场居中,从而在所有方向上扩展边框。

Now, I could go and find all the various draw methods and add the appropriate offset, or I could simply (though I do not know how) embed the view in another, and simply move 0,0 in the playfield by the appropriate offset. 现在,我可以找到所有各种绘制方法并添加适当的偏移量,或者我可以简单地(尽管我不知道如何)将视图嵌入到另一个视图中,然后在游戏场中简单地将0,0移动适当的偏移量。 The latter is my strongly preferred, but I simply don't know where to begin! 后者是我的首选,但我根本不知道从哪里开始!

Any ideas? 有任何想法吗?

If you are totally sure that your contents will fit the display and you just want to center it inside a layout, assuming that the size of your game is (eg 320x200) you could start from something like this: 如果您完全确定自己的内容适合显示屏,并且只想将其居中放置在布局中,那么假设您的游戏尺寸为(例如320x200),则可以从以下内容开始:

Just create a xml layout file with a root FrameLayout ( consider this a pointer: I didn't test this code ): 只需使用根FrameLayout创建一个xml布局文件(将其视为指针:我没有测试此代码 ):

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="#000000"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <YourView
        android:id="@+id/myview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"/>
</FrameLayout>

The game content should be centered on the screen and the black border of "YourView" will overflow the screen. 游戏内容应在屏幕上居中,“ YourView”的黑色边框将在屏幕上溢出。

Hope this helps 希望这可以帮助

NOTE : edited the layout_width/height parameters (as setting the content size inside them yields an incorrect result, unless your view is honoring something similar to android:gravity parameter). 注意 :编辑了layout_width / height参数(因为在其中设置内容大小会产生不正确的结果,除非您的视图支持类似于android:gravity参数的内容)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM