我有一个 RelativeLayout
,目前有一个 ImageView
,我希望将它对齐到屏幕的右上角,然后另一个 ImageView
到左上角。但是,目前,由于某种原因,应该容纳这些的 RelativeLayout
未与屏幕顶部对齐。
我有以下几点:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#a6c"
android:padding="16dp"
android:gravity="bottom"
android:id="@+id/frontFragment"
>
<TextView
android:id="@+id/frontText"
style="?android:textAppearanceLarge"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0"
android:duplicateParentState="true"
android:gravity="center"
android:textStyle="bold" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="45dip"
android:layout_gravity="top">
<ImageView
android:id="@+id/infoIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" issue here
android:src="@drawable/info" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="45dip"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@+id/frontDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="some value"
android:layout_alignParentBottom="true"/>
<TextView
android:id="@+id/frontCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="another value"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
</LinearLayout>
但输出如下
答案 0 :(得分:1)
正如评论中指出的那样,使用 ConstraintLayout
可以更轻松地完成,而无需嵌套布局。
如下,只需替换为您的内容(图标/文本):
<?xml version="1.0" encoding="utf-8"?>
<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/colorPrimary">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/home_selected" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_add" />
<TextView
android:id="@+id/frontDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginStart="8dp"
android:layout_marginBottom="8dp"
android:text="some value"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/frontCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="another value"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
结果如下:
答案 1 :(得分:1)
从线性布局中移除 android:gravity="bottom"
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#a6c"
android:padding="16dp"
android:gravity="bottom"
android:id="@+id/frontFragment"
>
到:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#a6c"
android:padding="16dp"
android:id="@+id/frontFragment"
>