Android Studio - 为什么所有控件都在左上角



为什么当我测试运行时,我所有的控件(按钮、文本字段(都在模拟器的左上角?

当您首次将widgets添加到应用程序时,它会自动转到屏幕的左上角。尝试在widgets中添加一些Constraints

若要在应用中心设置View位置,请将以下代码行添加到 XML 中widget的底部:

app:layout_constraintBottom_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="parent"

示例

<TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  app:layout_constraintBottom_toTopOf="parent"
  app:layout_constraintEnd_toEndOf="parent"
  app:layout_constraintStart_toStartOf="parent"
  app:layout_constraintTop_toBottomOf="parent"
/>

看起来您可能正在使用约束布局。 确保在布局中对每个控件设置了垂直和水平约束。

您的视图未在布局中正确设置。首先,如果你能为你的布局提供代码(activity_main.xml或类似的东西(,那么解决你的问题会更容易。

可能的罪魁祸首是您使用的是RelativeLayout并且没有将视图设置为与另一个视图相对位置,或者更有可能的是,您正在使用ConstraintLayout并且没有正确链接视图。

如果使用约束布局,链接视图的最简单方法是从"设计"选项卡查看应用的 UI。 单击您的TextView/EditText/etc,然后从侧面显示的小方块拖动到字面链接到屏幕一侧或另一个视图。

最新更新