[英]Android RelativeLayout placing 2 views next to eachother one view is variable?
[英]Android: center all views by one view in RelativeLayout
这是我要达到的目标的简化版本。 我在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>
看起来像我想要的:
但是当中心视图变宽时,左视图的行为会有所不同:
如果中心视图适合父级尺寸,则完全消失:
这就是我正在尝试做的事情:
如果有多余的空间
如果不是英语空间
有人可以提出一些建议吗?
我不知道您是否真的需要RelativeLayout
,也可以使用LinearLayout
但是我只是将布局更改为LinearLayout
将orientation
更改为horizontal
,并向HorizontalScrollView
添加了android:layout_weight="1"
,一切正常。 完整的代码看起来像这样,
<?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
并且可以像这样水平滚动
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.