文字变化时如何固定TextView的位置

时间:2019-05-16 22:46:36

标签: android android-layout

我的布局如下图所示,其中包含标题,某些文本视图和列表视图。标题与屏幕顶部对齐,列表与屏幕底部对齐,并且应该占用尽可能多的空间。

<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>

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

我该如何完成?我对任何布局类型都可以,我只是以上面的RelativeLayout代码为例。

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

2 个答案:

答案 0 :(得分:2)

您可以将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>

现在,标题的布局将显示为长文本或短文本:

enter image description here

enter image description here

答案 1 :(得分:0)

只需将此行添加到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" />