【问题标题】:How to fix TextView position when text in it changes文本更改时如何修复TextView位置
【发布时间】:2019-10-04 06:13:52
【问题描述】:

我有一个如下图所示的布局,包含一个标题、一些文本视图和一个列表视图。标题与屏幕顶部对齐,列表与屏幕底部对齐,应尽可能占用空间。

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MyActivity">

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="12dp"
        android:text="Title"
        android:textSize="30sp" />

    <TextView
        android:id="@+id/some_text"
        android:layout_below="@id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="6dp"
        android:text="Some random text which can change"
        android:textSize="14sp" />

    <ScrollView
        android:id="@+id/list_wrapper"
        android:layout_below="@id/some_text"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

Activity 创建后,标题下方的文本视图中的文本可以动态更改,当它发生时,它可能会缩小,列表视图可能会更改其大小。我想防止这种情况发生,以便列表视图保持其位置和大小,并且只有上方的文本视图会缩小并向下移动到列表中,如下图所示。

我怎样才能做到这一点?任何布局类型我都可以,我只是以上面的 RelativeLayout 代码为例。

如果列表视图可以具有固定大小会很容易,但我希望列表视图占用尽可能多的空间,并且在创建活动后我希望它保持原位而不改变其大小。

【问题讨论】:

  • 我想到的一个解决方案是创建两个文本视图,并在列表上方锚定第二个较小的文本视图并将其可见性设置为 false。然后稍后当文本必须更改时,我只需将第一个 textview 的可见性设置为 false 并将第二个 textview 的可见性设置为 true。但这是一个非常肮脏的解决方案,而且我不确定布局是否会尊重与隐藏项相关的位置约束。

标签: android android-layout


【解决方案1】:

您可以使用ConstraintLayoutguideline 来告诉您的列表视图和文本视图在屏幕上的确切位置(它将响应所有屏幕尺寸)。

看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
  <android.support.constraint.ConstraintLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

<ListView
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginBottom="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="@+id/guideline31" />

<TextView
    android:id="@+id/tes"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginBottom="8dp"
    android:text="Text"
    app:layout_constraintBottom_toTopOf="@+id/guideline31"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<android.support.constraint.Guideline
    android:id="@+id/guideline31"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    app:layout_constraintGuide_percent="0.2"/>
</android.support.constraint.ConstraintLayout>

现在您的布局看起来与标题的长文本或短文本相同:

【讨论】:

  • 这是好的解决方案,谢谢 Tamir。唯一不利的一面是,我必须为指南设置固定百分比值,而我希望指南位于带有起始文本的 textview 下方。
  • 你可以改变你的 textview heigyt 来包装内容,试试这个,如果这不起作用,我会给你一个例子
【解决方案2】:

只需在你的 textview xml 中添加这一行 android:maxLines="1"

 <TextView
        android:id="@+id/title"
        android:maxLines="1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="12dp"
        android:text="Title"
        android:textSize="30sp" />

【讨论】:

    猜你喜欢
    • 2022-12-13
    • 2012-05-28
    • 1970-01-01
    • 1970-01-01
    • 2012-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多