相对视图停留在布局顶部

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

我试图在我的RelativeLayout中放置一个ImageView,它位于屏幕下方3/4左右,但是一旦我将ImageView添加到RelativeLayout,布局和图像就会被捕捉到屏幕顶部而我不是确定如何从那里移动它。

这就是我每次向RelativeLayout添加ImageView时的样子

enter image description here

但我希望它位于“就绪”按钮的正上方

这是.xml

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="74dp"
    android:orientation="vertical"
    app:layout_constraintBottom_toTopOf="@+id/readyButton"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent">


    <ImageView
        android:id="@+id/player1FlipAvatar"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:contentDescription="@string/title_activity_flip_coin_lobby"
        tools:ignore="ContentDescription"
        tools:layout_editor_absoluteX="55dp"
        tools:layout_editor_absoluteY="40dp"
        tools:src="@drawable/defaultavatarmale" />
</RelativeLayout>
android android-layout android-relativelayout
2个回答
1
投票

发生这种情况是因为你不太了解RelativeLayout。

放置在RelativeLayout内的所有视图将自动放在RelativeLayout左上角的彼此之上。

如果要移动它,则需要“对齐”它。

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="74dp"
    android:orientation="vertical"
    app:layout_constraintBottom_toTopOf="@+id/readyButton"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent">


    <ImageView
        android:id="@+id/player1FlipAvatar"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_alignParentBottom="true"
        android:contentDescription="@string/title_activity_flip_coin_lobby"
        tools:ignore="ContentDescription"
        tools:layout_editor_absoluteX="55dp"
        tools:layout_editor_absoluteY="40dp"
        tools:src="@drawable/defaultavatarmale" />
</RelativeLayout>

通过将ImageView与RelativeLayout的底部对齐,如果RelativeLayout在Ready按钮正上方结束,则应将其置于Ready按钮上方。

如果要将ImageView居中,可以添加android:layout_centerHorizontal="true"

你可以在这里阅读更多关于RelativeLayout的信息:https://developer.android.com/guide/topics/ui/layout/relative

但是,有关xml代码的两个奇怪的事情。

  1. 为什么要在高度为74dp的RelativeLayout中放置高度为80dp的ImageView?它基本上是故意寻找麻烦。
  2. 如果您已经使用ConstraintLayout,为什么使用RelativeLayout?使用ConstraintLayout的主要好处之一是您不必使用嵌套布局。借助ConstraintLayout的强大功能和控制,您实际上可以将几乎所有视图重新排列到您想要的任何设计,而无需在其中嵌套另一个布局,如RelativeLayout。

我只是猜测你正在使用ConstraintLayout因为你在RelativeLayout中使用了app:layout_constraintStart_toStartOf而这些类型的'约束'仅存在于ConstraintLayout中。

因此,如果您已经使用ConstraintLayout并且您的Ready按钮位于ConstraintLayout内,您只需执行以下操作:

<ImageView
        android:id="@+id/player1FlipAvatar"
        android:layout_width="80dp"
        android:layout_height="80dp"
        app:layout_constraintBottom_toTopOf="@+id/readyButton"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        android:contentDescription="@string/title_activity_flip_coin_lobby"
        tools:ignore="ContentDescription"
        tools:layout_editor_absoluteX="55dp"
        tools:layout_editor_absoluteY="40dp"
        tools:src="@drawable/defaultavatarmale" />

您不需要RelativeLayout将ImageView置于就绪按钮之上。


0
投票

relative layout方向垂直包裹buttonlinear layout并随意调整重力

 <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:gravity="bottom"
       android:orientation="vertical">
   <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="74dp"
    android:orientation="vertical"
    app:layout_constraintBottom_toTopOf="@+id/readyButton"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent">


    <ImageView
        android:id="@+id/player1FlipAvatar"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:contentDescription="@string/title_activity_flip_coin_lobby"
        tools:ignore="ContentDescription"
        tools:layout_editor_absoluteX="55dp"
        tools:layout_editor_absoluteY="40dp"
        tools:src="@drawable/defaultavatarmale" />
    </RelativeLayout>
 </LinearLayout>
© www.soinside.com 2019 - 2024. All rights reserved.