无论方向如何都保持 RelativeLayout

Maintaining RelativeLayout regardless of orientation

我不确定我做错了什么。垂直视图正是我想要的,但水平视图变得扭曲。如何强制 ImageView 遵守第一个 TextView 下方的对齐方式?

Vertical view

Horizontal view

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.admin.firstcard.MainActivity">

<TextView
    android:id="@+id/firstLine"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="It's been a..."
    android:fontFamily="cursive"
    android:textSize="40sp"
    android:layout_alignParentTop="true"
    android:layout_margin="5dp" />

<ImageView
    android:id="@+id/dachshund"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:src="@drawable/dachshund"
    android:layout_below="@id/firstLine"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@id/dachshund"
    android:layout_alignParentRight="true"
    android:fontFamily="cursive"
    android:text="longgggggggggggg week"
    android:textSize="40sp"
    android:layout_margin="5dp" />

</RelativeLayout>

编辑:重新阅读 Android 文档我发现 alignBottom 是不正确的属性。

我认为您的主要问题是您的父布局的高度和宽度都设置为 "match_parent"。这意味着您的整个视图都被限制在设备屏幕的大小内,因此所有内容都被挤压。尝试将高度设置为 "wrap_content",然后应遵守对齐方式。

这将解决您的问题,如果还有错误,请继续发布!如果不成功,或使用线性布局以获得最佳结果

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/firstLine"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_margin="5dp"
        android:fontFamily="cursive"
        android:text="It's been a..."
        android:textSize="40sp" />

    <ImageView
        android:id="@+id/dachshund"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/firstLine"
        android:src="@drawable/ic_word" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/dachshund"
        android:layout_margin="5dp"
        android:fontFamily="cursive"
        android:text="longgggggggggggg week"
        android:textSize="40sp" />

</RelativeLayout>