android - 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>
但输出如下所示
解决方案
正如评论中指出的那样,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>
结果如下:
推荐阅读
- typescript - 使用多个导入接口扩展接口
- amazon-web-services - AWS Cognito 用户池:给定应用程序客户端 ID,如何获取其池 ID?
- pine-script - Pinescript 中的睡眠/暂停功能
- python - 姜戈。如何在查询结果中添加字段?
- regex - 基于模式的 Git 标签
- azure - 如何在 Blob 存储中添加事件网格过滤器?
- rspec - 无法使用 capybara 测试进行系统调用
- kotlin - 如何获取子属性/TwinCollection 的元数据/版本?
- python - 即使条件为真,if else 语句也会返回 else 条件 python
- flutter - 在 Flutter 循环进度指示器中显示下载百分比