RelativeLayout 隐藏之前的 LinearLayout

RelativeLayout hides prior LinearLayout

我有一个 XML 表示某些应用程序中的单行,包含名称、等级和图片。

名字+等级竖排在左边,我的意图是让图片在右边。

我正在尝试使用 RelativeLayout,以便将 ImageView 对齐到屏幕的右侧,据我所知,这是执行此操作的正确方法,但它隐藏了我的其他布局 -

使用时只有图像视图可见。

尝试更改布局 width\height 以匹配父级,但这没有帮助。

我误解了什么?我在这里的错误用法是什么?

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ll_line"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:background="#FF00">

    <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical"
        android:gravity="start|center">

        <TextView
            android:id="@+id/tv_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/tv_example"
            android:textSize="25sp"
            android:textStyle="bold"
            android:textAlignment="center"
            android:maxLines="1"
            android:ellipsize="end"
            android:padding="2dp"
            android:background="@color/transparent"
            android:focusable="false">
        </TextView>

        <TextView
            android:id="@+id/tv_grade"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="number"
            android:text="@string/tv_example"
            android:textSize="20sp"
            android:textStyle="bold|italic"
            android:textAlignment="center"
            android:maxLines="1"
            android:ellipsize="end"
            android:padding="2dp"
            android:background="@color/transparent"
            android:focusable="false">
        </TextView>
    </LinearLayout>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/iv_pic"
            android:layout_alignParentRight="true"/>
    </RelativeLayout>
</LinearLayout>

您是否尝试将权重总和设置为 first/root LinearLayout?如果没有,将 android:weightSum="3" 添加到根 LL。之后将 layout_weight="2" 添加到您的 RelativeLayout

试试这个:

把你的 child LinearLayout 像这样:

 <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:gravity="start|center"
        android:orientation="vertical">

你的 child RelativeLayout 像这样:

<RelativeLayout
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content">

我建议您对所有 XML 使用 RelativeLayout 或者您可以看看 ConstraintLayout.

使用RelativeLayout让一切变得简单

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ll_line"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="50dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/iv_pic"
        android:gravity="start|center"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@android:color/transparent"
            android:ellipsize="end"
            android:focusable="false"
            android:singleLine="true"
            android:padding="2dp"
            android:text="tv_exampleaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
            android:textAlignment="center"
            android:textSize="25sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/tv_grade"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@android:color/transparent"
            android:ellipsize="end"
            android:focusable="false"
            android:inputType="number"
            android:singleLine="true"
            android:padding="2dp"
            android:text="tv_example"
            android:textAlignment="center"
            android:textSize="20sp"
            android:textStyle="bold|italic" />
    </LinearLayout>


    <ImageView
        android:id="@+id/iv_pic"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignParentRight="true"
        android:background="#ff0000" />
</RelativeLayout>

希望这就是你想要的

您可以通过将父级 LinearLayout 替换为 FrameLayout 来轻松实现此目的。

但是为了在文本末尾显示...,您必须在现有布局中使用权重或静态宽度为 TextView 指定固定宽度。