首页 > 解决方案 > Android布局未与顶部对齐

问题描述

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>

但输出如下所示

在此处输入图像描述

标签: androidandroid-layoutimageview

解决方案


正如评论中指出的那样,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>

结果如下:

约束布局


推荐阅读