首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >安卓:在RelativeLayout中将所有视图集中在一个视图中

安卓:在RelativeLayout中将所有视图集中在一个视图中
EN

Stack Overflow用户
提问于 2016-02-21 17:38:59
回答 1查看 192关注 0票数 1

这是我试图实现的目标的简化版本。我在RelativeLayout中有两个视图。如果需要,一个视图 is stretched,另一个 one is to that view 并保持相同宽度。

这是一个简化的代码:

代码语言:javascript
复制
<?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

  • SECOND视图is TO

如果没有足够的空间

左侧是

  • 第二个视图CENTERED
  • FIRST视图占用所有剩余空间

有人能提个建议吗?

EN

回答 1

Stack Overflow用户

发布于 2016-02-21 18:02:48

我不知道您是否真的需要RelativeLayout,或者您也可以使用LinearLayout,但是我已经简单地将您的布局更改为LinearLayout,将orientation更改为horizontal,并将android:layout_weight="1"添加到HorizontalScrollView中,一切都很正常。完整的代码如下所示:

代码语言:javascript
复制
<?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,并且可以像这样水平滚动

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35534527

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档