【问题标题】:RelativeLayout hides prior LinearLayoutRelativeLayout 隐藏之前的 LinearLayout
【发布时间】:2017-12-16 17:08:05
【问题描述】:

我有一个 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


    【解决方案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>
    

    希望这是你想要的

    【讨论】:

    • 这有帮助,这种用法的唯一问题是TextView 中的很长的文本会使一些文本隐藏在图片后面,而不是在ImageView 之前以... 结尾
    • 我编辑了我的答案。使用 singleLine="true" 而不是 maxLines="1"。
    • 还是不行.. 我收到了&lt;text _start &lt;image&gt; text_end ...&gt;
    • 你看到我的预览了吗?只需复制整段代码并替换为您的代码即可。
    • 还没回家 :) 今晚会检查!谢谢
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      试试这个:

      把你的孩子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">
      

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

      【讨论】:

        【解决方案4】:

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

        【讨论】:

        • 现在试过了,TextViews 只显示 1 个字符,尽管行中有很多可用空间来显示整个文本,而 ImageView 向右对齐
        • 您可以更改线性和相对布局的 layout_weight 值以获得完美的结果,只需尝试一下即可。我认为如果您将 LinearLayout 的 layout_weight= 更改为 3 并将 relativeLayout 的更改为 1 会更好
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-03
        • 1970-01-01
        • 2023-03-18
        • 1970-01-01
        • 1970-01-01
        • 2013-12-26
        相关资源
        最近更新 更多