gpt4 book ai didi

android - 奇怪的布局问题

转载 作者:行者123 更新时间:2023-11-30 02:04:58 25 4
gpt4 key购买 nike

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

<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 似乎位于 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);

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

关于android - 奇怪的布局问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30816670/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com