Java 为什么组件一直在左上角?
我正在学习如何在android studio上构建一个简单的应用程序。 在视频中,他能够拖动他的组件或按钮在屏幕上的任何地方,无论是在中间,或右下角等。当我尝试,但是,它自动走到左上角。这是怎么回事,我该怎么解决?我可以手动更改一些属性,使它们位于它们应该位于的位置,但我想拥有它,这样我就可以随意拖放。请记住,我发布的代码与视频中的用户使用的代码相同Java 为什么组件一直在左上角?,java,android,xml,Java,Android,Xml,我正在学习如何在android studio上构建一个简单的应用程序。 在视频中,他能够拖动他的组件或按钮在屏幕上的任何地方,无论是在中间,或右下角等。当我尝试,但是,它自动走到左上角。这是怎么回事,我该怎么解决?我可以手动更改一些属性,使它们位于它们应该位于的位置,但我想拥有它,这样我就可以随意拖放。请记住,我发布的代码与视频中的用户使用的代码相同 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:and
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</RelativeLayout>
编辑:刚刚注意到,但我并不想包含代码的下半部分,因为当按钮被删除时,它被删除了。
以下是我和他在放置新组件之前的实际情况
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".MainActivity">
您使用的是
RelativeLayout
作为父布局,在TextView
中,您指定的约束仅适用于ConstraintLayout
将父布局更改为
ConstraintLayout
,您的约束将按预期工作。将TextView
的app:layout\u constraint…
属性替换为android:layout\u centerInParent=“true”
:
还有许多属性可以应用于
RelativeLayout
的子级。你是说android studio的UI编辑器吗?如果是,则不要使用它。这辆车又多又不可靠。只需手工编辑xml即可。此外,您的TextView
具有一些constraint
属性,当它是ConstraintLayout
的子对象时,这些属性适用。也许这就是问题所在?查看教程是否使用RelativeLayout
或ConstraintLayout
使用ConstraintLayout
。教程使用relative layoutHi,感谢您的回答,但我编辑了问题,其中显示了我开始使用的实际代码。新代码没有任何源代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Hello World!" />
</RelativeLayout>