RelativeView卡在布局顶部

时间:2019-01-14 23:06:23

标签: android android-layout android-relativelayout

我试图将ImageView放置在屏幕下方约3/4处的RelativeLayout内,但是,当我将ImageView添加到RelativeLayout时,布局和图像会立即吸附到屏幕顶部,并且我不确定如何从那里移动它。

每当我将ImageView添加到RelativeLayout时,它就是这样

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>

2 个答案:

答案 0 :(得分: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代码有2件事。

  1. 为什么要在高度为74dp的RelativeLayout中放置高度为80dp的ImageView?基本上是故意寻找麻烦。

  2. 如果已经使用ConstraintLayout,为什么要使用RelativeLayout?使用ConstraintLayout的主要好处之一是,您不必使用嵌套布局。借助ConstraintLayout的功能和控制,您实际上可以将几乎所有视图重新排列为所需的任何设计,而无需在其中嵌套另一个布局,例如RelativeLayout。

我只是猜测您使用的是ConstraintLayout,因为您在RelativeLayout中使用了app:layout_constraintStart_toStartOf,而这些类型的“约束”仅适用于ConstraintLayout。

因此,如果您已经在使用ConstraintLayout,并且“就绪”按钮位于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置于“就绪”按钮上方。

答案 1 :(得分:0)

class XcCsv(object): def __init__(self, field_delim, record_delim, quote_char, escape_char): self._field_delim = field_delim self._record_delim = record_delim self._quote_char = quote_char self._escape_char = escape_char self._records = [] self._record_buf = [] self._field_buf = "" self._in_quote = False self._in_escape = False # This could be different ... def _walker(self, data): data_length = len(data) if data_length == 1: self._parse_char(data) else: for d in data: self._walker(d) def _parse_char(self, char): if self._in_escape: self._field_buf += char self._in_escape = False elif char == self._escape_char: if self._in_quote: self._in_escape = True else: self._field_buf += char elif char == self._quote_char: if self._in_quote: if self._in_escape == True: self._field_buf += char else: self._in_quote = False else: self._in_quote = True elif char == self._field_delim: if self._in_quote: self._field_buf += char else: self._record_buf.append(self._field_buf) self._field_buf = "" elif char == self._record_delim: if self._in_quote: self._field_buf += char else: self._record_buf.append(self._field_buf) self._records.append(self._record_buf) self._record_buf = [] self._field_buf = "" else: self._field_buf += char def reader(self, data): self._walker(data) for rec in self._records: print(rec) csv = XcCsv("^","\n","'","\\") data = open("exotic_dialect.csv").readlines() csv.reader(data) 垂直包裹relative layoutbutton,并根据需要调整重力

linear layout
相关问题