在屏幕上定位TextViews

时间:2015-01-06 17:58:01

标签: android layout view

我的游戏有一块板,我想在每个黑色窗口上放置数字 Before After

      What my board looks like ----------------------> What I want to achieve

根据我用作背景的全高清图像,我知道哪些是必须放置数字的(x,y)坐标;由于所有分辨率的背景都相同,因此我根据设备屏幕大小应用缩放。然而,这些数字都放在错误的位置,彼此之间的距离不同,而不是垂直对齐。 Current result

以下是相关代码:

public class GraphicsActivity extends Activity implements SurfaceHolder.Callback {

private GraphicsSurfaceView view;
private static int[] player1positionX = {1556, 1325, 1101, 810, 587, 356};
private static int[] player1positionY = {886, 709, 886, 886, 709, 886};
private static int[] player2positionX = {356, 587, 810, 1101, 1325, 1556};
private static int[] player2positionY = {182, 361, 182, 182, 361, 182};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    Display display = getWindowManager().getDefaultDisplay();
    display.getRealSize(size);
    float scaleX = size.x / 960f;
    float scaleY = size.y / 540f;
    view = new GraphicsSurfaceView(this, scaleX, scaleY);
    LinearLayout bowlsPlayer1 = new LinearLayout(this);
    LinearLayout bowlsPlayer2 = new LinearLayout(this);
    gameBoard.addView(view);
    gameBoard.addView(bowlsPlayer1);
    gameBoard.addView(bowlsPlayer2);
    setContentView(gameBoard);

    /** Some code **/

    for (int i = 0; i < Game.BOWL_NUMBER; i++) {
        TextView bowl1 = new TextView(GraphicsActivity.this);
        bowl1.setText(String.valueOf(Game.INITIAL_SEED));
        bowl1.setTextColor(Color.WHITE);
        bowl1.setTextSize(40);
        bowl1.setX(player1positionX[i] / scaleX);
        bowl1.setY(player1positionY[i] / scaleY);
        bowlsPlayer1.addView(bowl1);
        TextView bowl2 = new TextView(GraphicsActivity.this);
        bowl2.setText(String.valueOf(Game.INITIAL_SEED));
        bowl2.setTextColor(Color.WHITE);
        bowl2.setTextSize(40);
        bowl2.setX(player2positionX[i] / scaleX);
        bowl2.setY(player2positionY[i] / scaleY);
        bowlsPlayer2.addView(bowl2);
    }
}

如何正确地将数字放在屏幕上?

编辑1:Der Golem建议后,我从pixel to dp切换;这对于我的坐标更好地工作是有用的。我也从LinealLayout切换到RelativeLayout,在其中插入数字,最后将它们全部放在相同的距离。 Actual result

如何将放置TextView中心的数字置于坐标而不是左上角?

0 个答案:

没有答案