我使用Android Studio进行编码。我发现一些问题,当我在Deisgner中进行设计时,模拟器和APK中的布局(在我的小工具中构建APK并运行)与布局不同。我的代码有什么问题?还是我必须设置一些东西?
我的XML代码:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
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="com.example.alfonsus.ebaandroid.Login">
<EditText
android:id="@+id/etPassword"
android:layout_width="280dp"
android:layout_height="41dp"
android:ems="10"
android:hint="Password"
android:inputType="textPersonName"
tools:layout_editor_absoluteX="52dp"
tools:layout_editor_absoluteY="113dp" />
<EditText
android:id="@+id/etUserId"
android:layout_width="280dp"
android:layout_height="41dp"
android:ems="10"
android:hint="User ID"
android:inputType="textPersonName"
tools:layout_editor_absoluteX="52dp"
tools:layout_editor_absoluteY="44dp" />
<Button
android:id="@+id/btnLogin"
android:layout_width="143dp"
android:layout_height="47dp"
android:text="Login"
tools:layout_editor_absoluteX="121dp"
tools:layout_editor_absoluteY="194dp" />
答案 0 :(得分:1)
布局中的每个视图都具有以下属性:
tools:layout_editor_absoluteX="52dp" tools:layout_editor_absoluteY="113dp"
任何tools:
属性将仅影响您在布局/设计预览中看到的内容,并且对您实际运行时看到的内容无效您在模拟器或真实设备上的应用。
在您的情况下,这些是在将视图拖放到可视化编辑器中时添加的(为了使您的生活更轻松;否则,这些视图将像在真实屏幕上看到的那样捕捉到左上角设备)。但是,您仍然需要向每个视图添加约束,以使它们在运行应用程序时正确显示。
有关更多信息,请参见此链接:https://developer.android.com/training/constraint-layout/#add-a-constraint
请注意,您必须遵循第3项:
执行下列操作之一:
单击约束手柄并将其拖动到可用的锚点(另一个视图的边缘,布局的边缘或准则)。
在“属性”窗口顶部的视图检查器中单击“创建连接”。
答案 1 :(得分:0)
这里的问题是您缺少一些约束。
再次检查您的android编辑器/设计器布局,我认为您缺少约束。 android中的每个视图都必须具有垂直和水平约束。
答案 2 :(得分:0)
您需要使每个项目都具有约束条件。