Android 布局未与顶部对齐
Android layout not being aligned to top
我有一个 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
你可以更轻松地完成它而无需嵌套布局。
如下,替换成你的内容(icons/text):
<?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>
结果如下:
从线性布局中删除 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"
>
我有一个 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
你可以更轻松地完成它而无需嵌套布局。
如下,替换成你的内容(icons/text):
<?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>
结果如下:
从线性布局中删除 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"
>