当我切换到RelativeLayout时,我在设计中放入的所有内容都会显示在屏幕的左上角

问题描述 投票:0回答:2

我在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" />

我想通过将它们拖到屏幕上而不将其卡在屏幕的左上角来重新放置它们。

android-studio android-relativelayout
2个回答
0
投票

是,这是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中指定它们,但是我发现有点困难。

  1. 单击一个小部件,然后该小部件周围将出现圆圈。
  2. 单击一个圆圈,按住。所有小部件都将带有圆圈。
  3. 将此圈子拖到另一个小部件的圈子中,如果您使用的是ConstraintLayout,则会在它们之间建立联系。
  4. 但是,如果您使用RelativeLayout,似乎不可能这样做,因为所有小部件都折叠到了同一位置。
  5. 请参见Android Guide to RelativeLayout以获取更多详细信息。


0
投票

谢谢,帮助了。我尝试投票,但没有足够的声誉。

© www.soinside.com 2019 - 2024. All rights reserved.