【问题标题】:Android: center all views by one view in RelativeLayoutAndroid:在RelativeLayout中以一个视图居中所有视图
【发布时间】: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


【解决方案1】:

我不知道你真的需要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 并可以像这样水平滚动

【讨论】:

  • 嗯,是的。我做了类似的事情。我只是将 single line 和 This is a text 设置为 wrap_content,效果很好。但我认为如果 This is a text 可以水平居中整个父级,那将是非常好的。
  • 单行会去哪里?你想隐藏它吗?
  • 像这样:如果 This is a text 宽度很小 -- my_picture1,否则如果 This is a text宽度很大——your_picture。 0_哇
  • 呵呵不,你不是,你可以问任何你喜欢的,没关系..看下面的文字是完全居中的。上面的文字显示得更宽,因为它的文字很长。如果它很短,那么它将显示为您的图片 1。
  • 它没有居中。 link 如果我使用 gravity=center 它不会是精确的屏幕中心
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-18
  • 1970-01-01
  • 2020-07-03
  • 1970-01-01
相关资源
最近更新 更多