繁体   English   中英

Android:在RelativeLayout中以一个视图居中所有视图

[英]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但是我只是将布局更改为LinearLayoutorientation更改为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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM