这是我试图实现的目标的简化版本。我在RelativeLayout中有两个视图。如果需要,一个视图 is stretched,另一个 one is to that view 并保持相同宽度。
这是一个简化的代码:
<?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>
看起来我想要:
但当中心视图较宽时,左视图的行为有所不同:
如果中心视图适合父大小,则完全消失:
对于那些感到困惑的人
这就是我正在尝试做的:
如果有额外的空间
第一个视图是CENTERED
如果没有足够的空间
左侧是
有人能提个建议吗?
发布于 2016-02-21 18:02:48
我不知道您是否真的需要RelativeLayout
,或者您也可以使用LinearLayout
,但是我已经简单地将您的布局更改为LinearLayout
,将orientation
更改为horizontal
,并将android:layout_weight="1"
添加到HorizontalScrollView
中,一切都很正常。完整的代码如下所示:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="horizontal">
<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:layout_weight="1"
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 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>
</LinearLayout>
当您的TextView文本开始变得更长时,它会占用右侧的空间,不会影响左侧的View
,并且可以像这样水平滚动
https://stackoverflow.com/questions/35534527
复制相似问题