尝试将视图定位在ConstraintLayout内部

3
我尝试在Android Studio中定位三个图像按钮。然而,每次我使用设计编辑器将图像按钮定位到正确的位置时,在运行应用程序时它不会生效。使用设计编辑器而不是文本编辑器会出现以下消息:
“此视图未受限制,仅具有设计时位置,因此除非添加约束条件,否则它将跳至(0,0)”。
如果我要在xml编辑器中添加约束条件,该怎么做?
此外,这行代码被添加到xml编辑器中: tools:layout_editor_absoluteY="80dp" tools:layout_editor_absoluteX="52dp" /> 我正在使用ConstraintLayout而不是RelativeLayout… 我的主.activity xml代码:
<ImageButton
    android:id="@+id/positive_Button"
    android:layout_width="150dp"
    android:layout_height="150dp"
    android:adjustViewBounds="true"
    android:background="@null"
    android:scaleType="fitCenter"
    app:srcCompat="@drawable/positive"
    android:gravity="left"
    tools:layout_editor_absoluteY="80dp"
    tools:layout_editor_absoluteX="39dp" />

<ImageButton
    android:id="@+id/neutral_Button"
    android:scaleType="fitCenter"
    android:layout_width="150dp"
    android:layout_height="150dp"
    app:srcCompat="@drawable/neutral"
    android:background="@null"
    android:adjustViewBounds="true"
    tools:layout_editor_absoluteY="80dp"
    tools:layout_editor_absoluteX="230dp" />

<ImageButton
    android:id="@+id/negative_Button"
    android:layout_width="150dp"
    android:layout_height="150dp"
    app:srcCompat="@drawable/negative"
    android:background="@null"
    android:adjustViewBounds="true"
    android:scaleType="fitCenter"
    tools:layout_editor_absoluteY="80dp"
    tools:layout_editor_absoluteX="412dp" />


你的布局文件中的 parent view 是什么? - AwaisMajeed
请发布您的 XML 代码。 - Abid Khan
请提供您的所有XML代码。 - Anmar Qashqish
ConstraintLayout 是父级布局吗? - Abid Khan
是的,ConstraintLayout 是我的父布局。 - Jens
1
@Jens 请尝试编辑您的问题标题为尝试在ConstraintLayout中定位视图,以便其他遇到同样问题的人可以找到解决方案。 - Abid Khan
3个回答

4
在编辑器的设计选项卡中,右键单击您的约束布局小部件,然后在下拉列表中单击Infer Constraints。IDE会自动为您添加代码。
Constraint Layout ---> Infer Constraint

1
属性tools:仅用于开发目的。构建时会剥离tools:属性。您希望如何定位视图?然后我可以提出一个解决方案。例如,您可以使用android:layout_marginLeft="80dp"来定位X。

0

enter image description here

MainActivity.java 中将 Activity 更改为 AppCompactActivity


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接