在嘗試以編程方式定位視圖之后,我注意到在我的設備(以及使用此應用程式的其他設備)中,視圖(0,0)被映射到右角而不是左。
在一個空白專案中,(0,0)是左上角,所以我包含了簡化版的 game_screen.xml 來顯示這個問題(包括 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"/span>
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"/span>>
<RelativeLayout。
android:id="@ id/lay"/span>
android:layout_width="match_parent"。
android:layout_height="match_parent"/span>>
<TextView。
android:id="@ id/text"。
android:layout_width="wrap_content"。
android:layout_height="wrap_content"。
android:text="Hello World!" />
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>/span>
activityMain.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(s 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"/span>
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"/span>>
<RelativeLayout。
android:id="@ id/gameLayout"
android:layout_width="match_parent"。
android:layout_height="match_parent"/span>>
<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>/span>
模擬器截圖:
個人設備螢屏截圖:
uj5u.com熱心網友回復:
從你提供的截圖中可以看出,你的設備配置被設定為RTL(從右到左)布局。
如果你想讓你的應用程式遵循設備設定的布局方向,那么請確保將android:supportedRtl="true"放在你的manifest.xml中。details
<application android:supportedRtl="true">
...
</application>
但是如果你希望你的應用程式不支持RTL(或設備布局方向),請在你的manifest.xml中加入android:supportedRtl="false";或者直接洗掉這個屬性,因為它默認為false。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/329566.html
標籤:
上一篇:如何為不同的用戶顯示不同的界面?
下一篇:C-本地變數具有相同的地址和值


