繁体   English   中英

两个视图之间的相对布局位置视图

[英]Relativelayout position view between two views

因此我目前正在开发Android上的应用程序,并且遇到了与RelativeLayout有关的特定问题,我找不到解决方法。

我在布局中具有三个视图,如下所示:TextView,Textview和ImageView(水平放置),这是ios对应屏幕截图:

在此处输入图片说明

中间的Textview应该坚持第一个,直到他到达Imageview为止,当他这样做时,他保持最小尺寸(包装内容),而第一个Textview截断。

在IOS上,我为约束设置了优先级以完成此任务,但我不知道如何在Android上解决此问题。

这是我尝试的:

<android.support.percent.PercentRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@drawable/daily_movie_title_box">

    <TextView
    android:id="@+id/daily_header_textview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"


    android:padding="15dp"
    android:text="New Text aawi oa ioawfwi"
    android:textSize="16sp"

    android:textStyle="bold"
    android:textColor="@color/white"

    android:lines="1"
    android:singleLine="true"

    android:layout_alignParentStart="true"
    />


    <TextView
    android:id="@+id/duration_text"
    android:text="138 mins"
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:textSize="13sp"
    android:textColor="@color/white"
    android:lines="1"
    android:layout_alignBaseline="@id/daily_header_textview"

    android:layout_toStartOf="@+id/certification_icon"
    android:layout_toEndOf="@id/daily_header_textview"

    />

    <ImageView
    android:id="@id/certification_icon"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:src="@drawable/uk12a"
    android:layout_alignBottom="@id/daily_header_textview"
    app:layout_aspectRatio="100%"/>

</android.support.percent.PercentRelativeLayout>

这导致了这个(这就是我想要的):

在此处输入图片说明

但是当我增加第一个Textview文本时,它的行为却不如我所愿...

在此处输入图片说明

是否可以实现我在Android中想要的行为(保留中间的Textview包装内容,并在需要时截断第一个内容)?

如果最终找到解决方案,我将发布更新,只是想看看是否有人能找到一种简单的方法来实现此目的,就像我怀疑的那样。

谢谢。

据我了解,您希望第一个TextView尽可能大,如果文本太小,则不要在文本后添加空格。 第二个TextView应该仅wrap_content ,但当行不填充时,它应填充父布局的其余部分。 ImageView设置为wrap_content

我使用以下布局对其进行了测试:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:shrinkColumns="0"
        android:stretchColumns="1">

        <TableRow>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:singleLine="true"
                android:text="Shrinking text dddddddddddddddddddddd"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Midle column"/>

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@mipmap/ic_launcher"/>
        </TableRow>

    </TableLayout>

</LinearLayout>

唯一的问题是,如果第二列的文本大得令人难以置信,它将其他视图从父级移出。 但是就您而言,我认为这不会成为问题。 否则,我认为它能胜任。

这些是一些建议的解决方案:

  • 您可以对每个组件( TextViewsImageView )使用具有水平方向和权重的LinearLayout
  • 您可以设置第二个TextView的最小和最大文本长度。

但我更喜欢应用第一个解决方案。 您可以使用以下方法为每个组件(屏幕上的空间量)分配权重:

android:layout_height

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM