简体   繁体   English

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

[英]Relativelayout position view between two views

so I'm currently working on an app on Android, and I got stuck on a specific problem regarding the RelativeLayout, which I can't find a way to solve. 因此我目前正在开发Android上的应用程序,并且遇到了与RelativeLayout有关的特定问题,我找不到解决方法。

I have in the layout three views as follows: TextView, Textview and ImageView (laid horizontally), here is a screenshot of the ios counterpart: 我在布局中具有三个视图,如下所示:TextView,Textview和ImageView(水平放置),这是ios对应屏幕截图:

在此处输入图片说明

the Textview at the middle should stick to the first one, until he gets to the Imageview, when he does, he keeps his minimum size (wrap content), while the first Textview truncate. 中间的Textview应该坚持第一个,直到他到达Imageview为止,当他这样做时,他保持最小尺寸(包装内容),而第一个Textview截断。

On IOS I setted priorities to the constraint to accomplish this, but I can't figure out how to solve this on Android. 在IOS上,我为约束设置了优先级以完成此任务,但我不知道如何在Android上解决此问题。

Here what I tried: 这是我尝试的:

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

Which resulted in this (which is what I want): 这导致了这个(这就是我想要的):

在此处输入图片说明

But when I increase the first Textview text it's not behaving as I desire... 但是当我增加第一个Textview文本时,它的行为却不如我所愿...

在此处输入图片说明

Is it possible to achieve the behaviour I want in Android (keep the middle Textview wrap content, and truncate the first one if needed)? 是否可以实现我在Android中想要的行为(保留中间的Textview包装内容,并在需要时截断第一个内容)?

I will post an update if I find a solution eventually, just wanted to see if anyone can find an easy way to achieve this behaviour, as I suspect there is. 如果最终找到解决方案,我将发布更新,只是想看看是否有人能找到一种简单的方法来实现此目的,就像我怀疑的那样。

Thanks. 谢谢。

From my understanding, you want the first TextView to be as large as possible, without adding space after the text if the text is too small. 据我了解,您希望第一个TextView尽可能大,如果文本太小,则不要在文本后添加空格。 The second TextView should only wrap_content , but it should fill the rest of the parent layout when the row doesn't. 第二个TextView应该仅wrap_content ,但当行不填充时,它应填充父布局的其余部分。 The ImageView is set to wrap_content . ImageView设置为wrap_content

I tested it with this layout: 我使用以下布局对其进行了测试:

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

The only problem is that if the second column has a incredibly large text, it will push the other views out of the parent. 唯一的问题是,如果第二列的文本大得令人难以置信,它将其他视图从父级移出。 But in your case, I don't think that will be a problem. 但是就您而言,我认为这不会成为问题。 Otherwise, I think it does the job. 否则,我认为它能胜任。

These are some suggested solutions: 这些是一些建议的解决方案:

  • You can use LinearLayout with horizontal orientation and weight for each component ( TextViews and ImageView ). 您可以对每个组件( TextViewsImageView )使用具有水平方向和权重的LinearLayout
  • You can set the minimum and maximum text length for the second TextView. 您可以设置第二个TextView的最小和最大文本长度。

But i prefer to apply the first solution. 但我更喜欢应用第一个解决方案。 You can assign a weight for each component ( amount of space on the screen ) using: 您可以使用以下方法为每个组件(屏幕上的空间量)分配权重:

android:layout_height

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

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