RelativeLayout隐藏了之前的LinearLayout

问题描述 投票:0回答:4

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

名称+等级垂直设置在左侧的行中,我的目的是让图片位于该行的右侧。

我正在尝试使用RelativeLayout,以便将ImageView对齐到屏幕的右侧,因为我知道这是正确的方法,但它隐藏了我的其他布局 -

使用时只能看到图像视图。

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

什么是我的误导?我的错误用法是什么?

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>
android android-layout android-linearlayout android-relativelayout
4个回答
1
投票

使用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>

希望这就是你想要的

enter image description here


0
投票

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

但是为了显示......在文本的最后,你必须使用现有布局中的重量或静态宽度给TextView一个固定的宽度。


0
投票

试试这个 :

把你的孩子LinearLayout这样:

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

你的孩子RelativeLayout是这样的:

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

我建议您使用RelativeLayout获取所有XML,或者您可以查看ConstraintLayout


0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.