我在Android Studio中从ConstraintLayout
切换为RelativeLayout
,由于某种原因,现在我在设计中输入的所有内容都显示在左上角。
我可以在Design
中将其拖动并重新放置,但是书写填充非常耗时且烦人。我什至将默认值更改为RelativeLayout
,但没有任何变化。
<?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:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
我想通过将它们拖到屏幕上而不将其卡在屏幕的左上角来重新放置它们。
是,这是RelativeLayout
的属性。
RelativeLayout
需要您告诉小部件之间的位置关系。如果未指定所有小部件的位置关系,则所有小部件都将折叠到父级的(0, 0)
,这将显示您看到的内容,>
我放入设计中的所有内容都显示在左上方。
要指定小部件之间的关系,您可以使用
android:layout_*
系列属性,例如:
<TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" /> <!-- this will at top left --> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/textView" android:text="Button" /> <!-- this will at right of textView --> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toBottomOf="@id/button" android:text="TextView" /> <!-- this will below button -->
您可以尝试在
Design Layout
中指定它们,但是我发现有点困难。
ConstraintLayout
,则会在它们之间建立联系。RelativeLayout
,似乎不可能这样做,因为所有小部件都折叠到了同一位置。请参见Android Guide to RelativeLayout以获取更多详细信息。
谢谢,帮助了。我尝试投票,但没有足够的声誉。