在Android Studio中,小部件在仿真期间在屏幕上向上移动

时间:2017-06-04 04:42:00

标签: android-layout android-studio

我是Stack Overflow和Android Studio的新手,现在正在学习编码。

我在Android Studio中进行了基本的用户和密码布局,如下所示:

layout i tried.

当我在模拟器上运行它时,它会显示如下小部件:

screen shot

我尝试更改EditText字段,按钮的宽度,但我尝试过的任何内容似乎都无法正常工作。

我该如何解决这个问题?

我错过了什么或者我做错了什么?

布局代码:

  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    android:weightSum="1">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="233dp"
        android:scaleType="fitStart"
        android:visibility="visible"
        app:srcCompat="@drawable/worldcurrency" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Enter ammount"
        android:textSize="18sp" />

    <EditText
        android:id="@+id/dollar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="numberDecimal" />

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="convert"
        android:text="Convert" />

    <TextView
        android:id="@+id/rupees"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

您的布局的父级是ConstraintLayout,但它缺少所有约束。 ConstraintLayout的特点是能够为每个视图设置约束,迫使其在不同大小和方向的屏幕上行为。

如果您真的想使用ConstraintLayout我建议您查看ConstraintLayout Documentation

正确设置约束后,您应该xml this {注意constraint*中的Views属性。<登记/>
因此,您的布局没有约束,它可能会以“随机”位置结束。此外,您已使用tools设置视图坐标,这仅适用于布局预览,而不适用于运行时。此外,建议不要使用absolute坐标,因为它可能会导致不同屏幕尺寸的问题
如果ConstraintLayout是您的可选选项,您可以将布局父级更改为LinearLayout vertical方向,我认为它更适合您正在构建的布局(我通过你的截图假设这个。)
希望我帮忙,让我知道。祝你好运,编码愉快!