两个视图之间的相对布局位置视图
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 换行内容,并在需要时截断第一个内容)?
如果我最终找到解决方案,我会 post 更新,只是想看看是否有人可以找到实现此行为的简单方法,正如我怀疑的那样。
谢谢。
这些是一些建议的解决方案:
- 您可以对每个组件(
TextViews
和 ImageView
)使用水平方向和权重的 LinearLayout
。
- 您可以设置第二个 TextView 的最小和最大文本长度。
但我更喜欢应用第一种解决方案。您可以使用以下方法为每个组件分配权重(屏幕上 space 的数量):
android:layout_height
据我了解,您希望第一个 TextView
尽可能大,如果文本太小,则不要在文本后添加 space。第二个 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>
唯一的问题是,如果第二列的文本非常大,它会将其他视图推出父视图。但就您而言,我认为这不是问题。否则,我认为它可以完成工作。
所以我目前正在 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 换行内容,并在需要时截断第一个内容)?
如果我最终找到解决方案,我会 post 更新,只是想看看是否有人可以找到实现此行为的简单方法,正如我怀疑的那样。
谢谢。
这些是一些建议的解决方案:
- 您可以对每个组件(
TextViews
和ImageView
)使用水平方向和权重的LinearLayout
。 - 您可以设置第二个 TextView 的最小和最大文本长度。
但我更喜欢应用第一种解决方案。您可以使用以下方法为每个组件分配权重(屏幕上 space 的数量):
android:layout_height
据我了解,您希望第一个 TextView
尽可能大,如果文本太小,则不要在文本后添加 space。第二个 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>
唯一的问题是,如果第二列的文本非常大,它会将其他视图推出父视图。但就您而言,我认为这不是问题。否则,我认为它可以完成工作。