【问题标题】:Maintaining RelativeLayout regardless of orientation无论方向如何,都保持 RelativeLayout
【发布时间】:2018-02-11 13:59:05
【问题描述】:

我不确定我做错了什么。垂直视图正是我想要的,但是水平视图变得扭曲了。如何强制 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>

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

【问题讨论】:

    标签: android xml layout orientation


    【解决方案1】:

    这将解决您的需要,如果进一步错误,请继续发布!或者如果不成功则使用线性布局以获得最佳效果

    <?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>
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 我试过了,还是不行。我确实注意到我的代码中有一个错误。我让 ImageView 与 ParentTop 对齐。现在它与第一个 TextView 对齐,它的布局更好。除了现在第二个 TextView 从屏幕上掉下来
      猜你喜欢
      • 2021-12-03
      • 1970-01-01
      • 1970-01-01
      • 2014-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-27
      • 2022-01-16
      相关资源
      最近更新 更多