【发布时间】:2016-02-21 09:38:58
【问题描述】:
这是我想要实现的简化版本。我在 RelativeLayout 中有两个视图。 一个视图始终居中并在需要时拉伸,另一个始终向左 > 该视图并保持宽度相同。
这是一个简化的代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Single line"
android:layout_toLeftOf="@+id/horiz"
android:layout_marginRight="8dp"
android:layout_centerVertical="true"
android:textSize="20sp"/>
<HorizontalScrollView
android:id="@+id/horiz"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:fadeScrollbars="false"
android:fillViewport="true">
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is a text"
android:textSize="17sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is a text"
android:textSize="17sp"/>
</TableLayout>
</HorizontalScrollView>
看起来我想要:
但是当中心视图更宽时,左视图的行为会有所不同:
如果中心视图适合父尺寸,则完全消失:
对于那些感到困惑的人
这就是我想要做的:
如果有额外的空间
- 第一个视图居中
- 第二个视图是向左到第一个
如果没有足够的空间
- 第二个视图位于左侧居中
- 第一个视图占用所有剩余空间
有人可以提议吗?
【问题讨论】:
-
在 horrizontalscolview 中添加 android:layout_toRightOf="@+id/yourtextviewid" 并删除 android:layout_toLeftOf="@+id/horiz"
-
水平视图未居中
标签: android android-layout android-xml android-relativelayout