奇怪的布局问题

Strange layout issue

我正在使用相对布局,这是从中截取的部分:

<ImageView
    android:id="@+id/btnLifeMinus5"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:background="#2a80b9"
    android:visibility="invisible"
    android:adjustViewBounds="false"
    android:clickable="true"
    android:cropToPadding="false"
    android:padding="0dp"
    android:scaleType="fitStart"
    android:layout_marginLeft="0dp"
    android:layout_marginTop="0dp" />

<ImageView
    android:id="@+id/btnLifePlus5"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:background="#2a80b9"
    android:visibility="invisible"
    android:adjustViewBounds="false"
    android:clickable="true"
    android:cropToPadding="false"
    android:padding="0dp"
    android:scaleType="fitStart"
    android:layout_marginLeft="0dp"
    android:layout_marginTop="0dp" />

<ImageView
    android:id="@+id/btnLifePlus1"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:background="#2a80b9"
    android:visibility="invisible"
    android:adjustViewBounds="false"
    android:clickable="true"
    android:cropToPadding="false"
    android:padding="0dp"
    android:scaleType="fitStart"
    android:layout_marginLeft="0dp"
    android:layout_marginTop="0dp" />

<ImageView
    android:id="@+id/btnLifeMinus1"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:background="#2a80b9"
    android:visibility="invisible"
    android:adjustViewBounds="false"
    android:clickable="true"
    android:cropToPadding="false"
    android:padding="0dp"
    android:scaleType="fitStart"
    android:layout_marginLeft="0dp"
    android:layout_marginTop="0dp" />

<TextView
    android:id="@+id/txtLifePlus5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="invisible"
    android:adjustViewBounds="false"
    android:clickable="true"
    android:cropToPadding="false"
    android:textColor="#000000"
    android:text="+5"
    android:padding="0dp" />

<TextView
    android:id="@+id/txtLifePlus1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="invisible"
    android:adjustViewBounds="false"
    android:clickable="true"
    android:cropToPadding="false"
    android:textColor="#000000"
    android:text="+1"
    android:padding="0dp" />

<TextView
    android:id="@+id/txtLifeMinus5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="invisible"
    android:adjustViewBounds="false"
    android:clickable="true"
    android:cropToPadding="false"
    android:textColor="#000000"
    android:text="-5"
    android:padding="0dp" />

<TextView
    android:id="@+id/txtLifeMinus1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="invisible"
    android:adjustViewBounds="false"
    android:clickable="true"
    android:cropToPadding="false"
    android:textColor="#000000"
    android:text="-1"
    android:padding="0dp" />

我的 class 中有以下方法可以在屏幕上设置这些按钮。此部分将文本放置在按钮上:

private void setTextPlacements() {
    final float SCALE = getResources().getDisplayMetrics().density;
    final TextView txtPlus1 = (TextView) findViewById(R.id.txtLifePlus1);
    final TextView txtPlus5 = (TextView) findViewById(R.id.txtLifePlus5);
    final TextView txtMinus1 = (TextView) findViewById(R.id.txtLifeMinus1);
    final TextView txtMinus5 = (TextView) findViewById(R.id.txtLifeMinus5);
    final ImageView btnPlus5 = (ImageView) findViewById(R.id.btnLifePlus5);
    final ImageView btnPlus1 = (ImageView) findViewById(R.id.btnLifePlus1);
    final ImageView btnMinus5 = (ImageView) findViewById(R.id.btnLifeMinus5);
    final ImageView btnMinus1 = (ImageView) findViewById(R.id.btnLifeMinus1);

    RelativeLayout.LayoutParams txtP1 = new RelativeLayout.LayoutParams(txtPlus1.getLayoutParams());
    txtP1.leftMargin = Math.round((btnPlus1.getLeft() + (btnPlus1.getWidth()/2)) - (txtPlus1.getWidth()/2));
    txtP1.topMargin = Math.round((btnPlus1.getTop() + (btnPlus1.getHeight()/2)) - (txtPlus1.getHeight()/2));
    txtPlus1.setLayoutParams(txtP1);

    RelativeLayout.LayoutParams txtP5 = new RelativeLayout.LayoutParams(txtPlus5.getLayoutParams());
    txtP5.leftMargin = Math.round((btnPlus5.getLeft() + (btnPlus5.getWidth()/2)) - (txtPlus5.getWidth()/2));
    txtP5.topMargin = Math.round((btnPlus5.getTop() + (btnPlus5.getHeight()/2)) - (txtPlus5.getHeight()/2));
    txtPlus5.setLayoutParams(txtP5);

    RelativeLayout.LayoutParams txtM1 = new RelativeLayout.LayoutParams(txtMinus1.getLayoutParams());
    txtM1.leftMargin = Math.round((btnMinus1.getLeft() + (btnMinus1.getWidth()/2)) - (txtMinus1.getWidth()/2));
    txtM1.topMargin = Math.round((btnMinus1.getTop() + (btnMinus1.getHeight()/2)) - (txtMinus1.getHeight()/2));
    txtMinus1.setLayoutParams(txtM1);

    RelativeLayout.LayoutParams txtM5 = new RelativeLayout.LayoutParams(txtMinus5.getLayoutParams());
    txtM5.leftMargin = Math.round((btnMinus5.getLeft() + (btnMinus5.getWidth()/2)) - (txtMinus5.getWidth()/2));
    txtM5.topMargin = Math.round((btnMinus5.getLeft() + (btnMinus5.getWidth()/2)) - (txtMinus5.getWidth()/2));
    txtMinus5.setLayoutParams(txtM5);
}

此应用程序中发生了很多事情,除一件事外,一切都运行良好:txtLifeMinus5 似乎位于 btnLifeMinus5 下方,我就是不明白为什么。一切设置都与我在整个应用程序的布局中所做的其他一切设置相同,但这个行为不正常。

我的假设是在布局 XML 文档中较高位置列出的项目位于其下方的所有项目的下方...那么这里到底发生了什么?

文本不可见不是问题,因为我已经尝试在屏幕上的不同位置显示它。一旦它位于 btnLifeMinus5 按钮上方,它就会消失在下方。

啊!有什么想法吗?

天哪,我是个白痴。对不起大家。答案很简单,布局顺序完全没有错误。此代码:

RelativeLayout.LayoutParams txtM5 = new RelativeLayout.LayoutParams(txtMinus5.getLayoutParams());
txtM5.leftMargin = Math.round((btnMinus5.getLeft() + (btnMinus5.getWidth()/2)) - (txtMinus5.getWidth()/2));
txtM5.topMargin = Math.round((btnMinus5.getLeft() + (btnMinus5.getWidth()/2)) - (txtMinus5.getWidth()/2));
txtMinus5.setLayoutParams(txtM5);

与所有此代码不同:

RelativeLayout.LayoutParams txtP1 = new RelativeLayout.LayoutParams(txtPlus1.getLayoutParams());
txtP1.leftMargin = Math.round((btnPlus1.getLeft() + (btnPlus1.getWidth()/2)) - (txtPlus1.getWidth()/2));
txtP1.topMargin = Math.round((btnPlus1.getTop() + (btnPlus1.getHeight()/2)) - (txtPlus1.getHeight()/2));
txtPlus1.setLayoutParams(txtP1);

RelativeLayout.LayoutParams txtP5 = new RelativeLayout.LayoutParams(txtPlus5.getLayoutParams());
txtP5.leftMargin = Math.round((btnPlus5.getLeft() + (btnPlus5.getWidth()/2)) - (txtPlus5.getWidth()/2));
txtP5.topMargin = Math.round((btnPlus5.getTop() + (btnPlus5.getHeight()/2)) - (txtPlus5.getHeight()/2));
txtPlus5.setLayoutParams(txtP5);

RelativeLayout.LayoutParams txtM1 = new RelativeLayout.LayoutParams(txtMinus1.getLayoutParams());
txtM1.leftMargin = Math.round((btnMinus1.getLeft() + (btnMinus1.getWidth()/2)) - (txtMinus1.getWidth()/2));
txtM1.topMargin = Math.round((btnMinus1.getTop() + (btnMinus1.getHeight()/2)) - (txtMinus1.getHeight()/2));
txtMinus1.setLayoutParams(txtM1);

看到了吗?失败的部分在它应该引用高度的地方引用了宽度。这会将文本 object 发送到屏幕外。史诗逻辑失败。抱歉浪费时间:(