【问题标题】:Long Android TextView pushes other views off-screen长的 Android TextView 将其他视图推送到屏幕外
【发布时间】:2019-10-11 01:51:03
【问题描述】:

我有两个并排的 TextView。 TextView1 有不同长度的文本,TextView2 总是显示“+#”。但是,当 TextView1 变长时,它会将 TextView2 推离屏幕。任何想法如何解决这一问题?这是我的布局代码:

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/TextView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:ellipsize="end"
            android:textSize="13sp"/>

        <TextView
            android:id="@+id/TextView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:textSize="13sp"/>

    </RelativeLayout>

【问题讨论】:

  • 您打算将父视图设为RelativeLayout 还是LinearLayout?您正在指定 android:orientation="horizo​​ntal" 并且方向仅对 LinearLayout 有效。此外,如果您能阐明预期的行为是什么,这将有所帮助。

标签: android android-layout android-relativelayout


【解决方案1】:

这实际上是我已经尝试解决了一段时间的问题。不幸的是,其他人建议的方法——在LinearLayout 中使用layout_weight——实际上并不起作用;不过,我已经为您找到了解决方案!

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="left">

    <TextView
        android:id="@+id/TextView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/TextView2"
        android:singleLine="true"
        android:ellipsize="end"
        android:textSize="13sp"/>

    <TextView
        android:id="@+id/TextView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:singleLine="true"
        android:textSize="13sp"/>
</RelativeLayout>

对于上面的块,我们使用RelativeLayout 来将第一个TextView 与第二个TextView 的左侧对齐。我们还将第二个TextView 对齐到父ViewGroup 的右侧。最后,我们将android:gravity="left" 添加到父ViewGroup,以便将所有TextView 对齐到左侧。

这会导致两个 TextView 并排 - 无论第一个 TextView 的长度如何。如果您希望第一个 TextView 包含多行,只需删除 android:ellipsize="end" 标记即可。

希望这是您的预期结果!

【讨论】:

  • 完美!!非常感谢,这正是我所需要的!
  • 最佳答案!我找了两个小时:)
  • @EvanBashir layout_weight 应该可以工作。请务必注意,如果您使用权重,则需要将 layout_heightlayout_width 设置为 0dp。如果不这样做,布局将不会调整子视图的宽度。
  • @Justin 您的评论与问题无关。是的,layout_weight 确实在 linear_layout 内工作;但是,使用layout_weight 来完成发帖人的请求是行不通的。
  • 这行得通,但它修复了最右边的所有内容,而我需要它位于父 RelativeLayout 的左侧......有什么帮助吗?
【解决方案2】:

在子视图上使用带有权重属性的 LinearLayout

<LinearLayout
  android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  xmlns:android="http://schemas.android.com/apk/res/android">

        <TextView
            android:id="@+id/TextView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:ellipsize="end"
            android:textSize="13sp"
            android:layout_weight="1"/>

        <TextView
            android:id="@+id/TextView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:textSize="13sp"
            android:layout_weight="0"/>

    </LinearLayout>

【讨论】:

    【解决方案3】:

    这个问题可以用android.support.constraint.ConstraintLayout解决

    这里是屏幕截图,因此您可以看到它在处理小而长的文本时的表现。

    这是 XML:

    <?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"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="15dp">
    
        <ScrollView
            android:id="@+id/scrollView"
            android:background="@drawable/white_rounded_rectangle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:scrollbars="none">
    
            <TextView
                android:id="@+id/messageTv"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fontFamily="sans-serif"
                android:lineSpacingExtra="6sp"
                android:paddingTop="15dp"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:paddingBottom="70dp"
                android:text="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever PageMaker including versions of Lorem Ipsum."
                android:textColor="#252525"
                android:textIsSelectable="true"
                android:textSize="@dimen/text_size16"
                />
        </ScrollView>
    
    
        <android.support.constraint.ConstraintLayout
            android:id="@+id/bottomLayout"
            android:layout_width="match_parent"
            android:layout_height="84dp"
            android:background="@drawable/white_rounded_rectangle"
            app:layout_constraintBottom_toBottomOf="@+id/scrollView"
            app:layout_constraintEnd_toEndOf="@+id/scrollView"
            app:layout_constraintStart_toStartOf="@+id/scrollView">
    
            <android.support.v7.widget.AppCompatButton
                android:id="@+id/btnAction1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginEnd="16dp"
                android:layout_marginBottom="19dp"
                android:layout_weight="1"
                android:text="Action 1"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent" />
    
            <android.support.v7.widget.AppCompatButton
                android:id="@+id/btnAction2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginEnd="17dp"
                android:layout_marginBottom="16dp"
                android:text="Action 2"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toStartOf="@+id/btnAction1" />
        </android.support.constraint.ConstraintLayout>
    
    </android.support.constraint.ConstraintLayout>
    

    【讨论】:

      猜你喜欢
      • 2015-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-04
      相关资源
      最近更新 更多