You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Studio应用坐标(0,0)映射为右上角而非左上角问题求助

我在通过代码动态设置View位置时发现,我自己的设备和其他安装了该应用的设备上,坐标(0,0)对应屏幕右上角,而不是常规的左上角。

空白测试项目中(0,0)可以正常对应左上角,因此我附上简化版的相关布局和代码用于复现问题:实际项目最早是在引入gameLayout后出现该问题,但即使后续移除了该布局,问题依然存在。

本次测试运行的API版本为25。

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <RelativeLayout
        android:id="@+id/lay"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!" />
    </RelativeLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

package com.benmassarano.mirroredscreendemo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.gridlayout.widget.GridLayout;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RelativeLayout lay = findViewById(R.id.lay);
        TextView text = findViewById(R.id.text);
        positionView(lay, text, RelativeLayout.LayoutParams.WRAP_CONTENT,
                     RelativeLayout.LayoutParams.WRAP_CONTENT);

    }

    private void positionView(RelativeLayout parent, View view, int widthSetting,
                              int heightSetting) {
        parent.post(new Runnable() {
            @Override
            public void run() {
                RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(widthSetting,
                                                                                     heightSetting);
                params.setMargins(0, 0, 0, 0);
                view.setLayoutParams(params);
            }
        });
    }
}

game_screen.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:id="@+id/gameLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/timer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="30s"
            android:textSize="28sp" />

    </RelativeLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

测试截图

  • 模拟器截图:emulator
  • 实体设备截图:device

内容的提问来源于stack exchange,提问作者benjamin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 02:45:02