2016-08-19 84 views
0

所以我目前正在研究Android上的一个应用程序,并且我遇到了有关RelativeLayout的特定问题,我无法找到解决方法。两个视图之间的Relativelayout位置视图

我在布局三个视图如下:TextView的,TextView的和ImageView的(水平放置),这里是IOS对应的截图:

enter image description here

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> 

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

enter image description here

但是,当我增加它不表现为我的第一个TextView的文本欲望...

enter image description here

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

我会发布更新,如果我最终找到解决方案,只是想看看是否有人可以找到一个简单的方法来实现这种行为,因为我怀疑有。

谢谢。

+0

您可以尝试将所有视图的宽度设置为“0dp”,然后使用属性layout_weight,然后将其与每个视图的百分比值(例如,第一视图5,下一个2和最后一个视图1)相关联。不知道这是你在找什么。 – TSGames

+0

在dp中为第一个TextView定义一个固定的layout_width。 –

+0

但我希望第一个Textview的宽度是动态的,以包装它的内容,除非没有空间可用,在这种情况下截断。 原因是我希望中间Textview在第一个结尾。 在当前场景中虽然中间文本截断。 – Nadav96

回答

1

从我的理解,你想第一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> 

唯一的问题是,如果第二列有一个令人难以置信的大文本,这将推动其他视图出父。但就你而言,我不认为这会是一个问题。否则,我认为它能完成这项工作。

+0

是的!似乎完全按照我的意愿行事(你是对的,中间的文字不应该太长)。 谢谢:) – Nadav96

0

以下是一些建议的解决方案:

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

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

android:layout_height 
+0

是的,我明白了,但使用第一种方法使第一个Textview的宽度为静态,无论其中的文本如何。 我想对齐中间的textview在第一个结尾,所以他们会看起来像他们在同一行。 我已经尝试了第二种方法,但似乎布局在设置其他约束(toStartOf,toEndOf)时会忽略此属性。 – Nadav96