我是Stack Overflow和Android Studio的新手,现在正在学习编码。
我在Android Studio中进行了基本的用户和密码布局,如下所示:
当我在模拟器上运行它时,它会显示如下小部件:
我尝试更改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>
答案 0 :(得分:0)
您的布局的父级是ConstraintLayout
,但它缺少所有约束。 ConstraintLayout
的特点是能够为每个视图设置约束,迫使其在不同大小和方向的屏幕上行为。
如果您真的想使用ConstraintLayout
我建议您查看ConstraintLayout Documentation。
正确设置约束后,您应该xml
this {注意constraint*
中的Views
属性。<登记/>
因此,您的布局没有约束,它可能会以“随机”位置结束。此外,您已使用tools
设置视图坐标,这仅适用于布局预览,而不适用于运行时。此外,建议不要使用absolute
坐标,因为它可能会导致不同屏幕尺寸的问题
如果ConstraintLayout
是您的可选选项,您可以将布局父级更改为LinearLayout
vertical
方向,我认为它更适合您正在构建的布局(我通过你的截图假设这个。)
希望我帮忙,让我知道。祝你好运,编码愉快!