奇怪的布局问题

时间:2015-06-13 08:30:18

标签: android layout

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

<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" />

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

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似乎坐在UNDERNEATH btnLifeMinus5,我无法弄清楚原因。所有内容都与我在整个应用程序中的布局中完成的所有内容完全相同,但是这个内容并没有正常运行。

我正在假设布局XML文档中列出的项目位于UNDERNEATH下面的所有项目...那么heckcheese在这里发生了什么?

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

哎呀!有什么想法吗?

1 个答案:

答案 0 :(得分:1)

天啊,我是个白痴。对不起大家。答案很简单,布局顺序根本没有错误。这段代码:

Select qtysold, sellprice, discount
from salestable
where receipt = '31103'

与所有此代码不同:

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);

看到了吗?失败部分引用应该引用高度的宽度。这会将文本对象从屏幕上移除数英里。史诗逻辑失败。抱歉浪费时间:(