我想要在 android xml 中相对布局左侧的两个元素和同一相对布局右侧的两个元素。我尝试如下。这不起作用。
我的 XML
<androidx.constraintlayout.widget.ConstraintLayout 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"
android:background="@color/primary_color"
android:id="@+id/linear_parentPl"
tools:context=".PlayList">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:orientation="vertical"
android:background="@drawable/top_bar">
<ImageView
android:id="@+id/back_iconPl"
android:layout_width="44dp"
android:layout_height="44dp"
android:src="@drawable/baseline_arrow_back_ios_24"
android:layout_marginLeft="16dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/play_list"
android:layout_toRightOf="@+id/back_iconPl"
android:textColor="@color/light_orange"
android:textStyle="bold"
android:textSize="24sp"
android:layout_marginTop="10dp"/>
<TextView
android:id="@+id/logout_text"
android:layout_toLeftOf="@+id/logout_iconPl"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/logout_text"
android:textColor="@color/white"
android:textStyle="bold"
android:textSize="14sp"
android:layout_marginTop="10dp"/>
<ImageView
android:id="@+id/logout_iconPl"
android:layout_alignParentRight="true"
android:layout_width="26dp"
android:layout_height="26dp"
android:src="@drawable/logoutrounded"/>
</RelativeLayout>
任何机构可以帮助我实现这一目标吗?
我的输出是
我建议你放弃相对布局并直接使用约束布局。 给出一些约束应该可以将对象放置在右端。
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/xxx"
app:layout_constraintTop_toBottomOf="parent"
其中“@id/xxx”是左侧对象的id。 如果您有多个左侧对象,请将它们包装到另一个约束布局中,并为该约束布局提供一个 id 来引用它。
此外,还可能需要 0dp 的宽度或高度,例如:
android:layout_width="0dp"
android:layout_height="match_parent"
这将填充宽度或高度的空白空间(两者均不可用)。 详细信息请参见 layout_height="0dp" 是什么意思?