Android使左侧的textview缩短

Android使左侧的textview缩短

本文介绍了Android使左侧的textview缩短,而右侧保持完全可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要水平显示两个单行TextView.左侧的TextView(将其命名为1)可能具有更长的文本,该文本可能会缩短并以"..."结尾.正确的文本视图(2)包含短文本,永远不应缩短.

I need to display two single-line TextViews horizontally. The left TextView (let's name it 1) may have a longer text which may shortened and finished with "...". The right text view (2) has a short text and should never get shortened.

我希望1保持与父对象的左端对齐. 2与1的右侧对齐.

I want the 1 to remain aligned to the left end of the parent. The 2 aligned to the right side of 1.

现在我必须满足两个条件

There are now 2 conditions that I have to meet

a)如果1的文字较短,则2应该与1的右边对齐(所有的都不会缩短).

a) if the 1 has a short text then the 2 should get aligned to the right of 1 (none of the gets shortened).

b),但如果1的文本太长,则应将视图1的文本缩短为"...",同时视图2会最大程度地移到父视图的右侧,但仍保持完全可见(否. )

b) but if the 1 has a too long text then the text of 1 should be shortened by '...' while the view 2 is moved maximally to the right of the parent but still remains fully visible (no ...)

我当前的解决方法如下.场景b)对于我来说很好,但是在a)的情况下,问题是视图2移到了父视图的右侧,视图1移到了左侧-两者都很短,并且两者之间有很大的空间看起来很奇怪我希望2移到最左侧(紧邻1),并在右侧保留此空间.

My current solution is the following below. The scenario b) is fine with mine, but in case of a) the problem is that the view 2 is moved to the right side of the parent and the 1 to the left side - both are short and there's pretty much space in between which looks odd. I want 2 to move to the further left (next to 1) and leave this space on the right side.

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_marginTop="1dp"
    android:layout_weight="0.5">

    <TextView
        android:id="@+id/ns_in_txt"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:gravity="top|left"
        android:maxLines="1"
        android:singleLine="true"
        android:textColor="#00a2ff"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/ns_txt"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@id/ns_in_txt"
        android:gravity="top|left"
        android:maxLines="1"
        android:singleLine="true"
        android:textColor="#00a2ff"
        android:textSize="18sp" />
</RelativeLayout>

推荐答案

尝试执行此操作

<RelativeLayout
     android:orientation="horizontal"
     android:layout_width="fill_parent"
     android:layout_height="wrap_context"
     android:layout_marginTop="1dp">

     <TextView
          android:id="@+id/ns_in_txt"
          android:layout_width="0dp"
          android:layout_height="match_parent"
          android:layout_weight="1"
          android:layout_alignParentRight="true"
          android:gravity="left"
          android:maxLines="1"
          android:ellipsize="end"
          android:singleLine="true"
          android:textColor="#00a2ff"
          android:textSize="18sp" />

     <TextView
          android:id="@+id/ns_txt"
          android:layout_width="wrap_content"
          android:layout_height="match_parent"
          android:layout_alignParentLeft="true"
          android:layout_toLeftOf="@id/ns_in_txt"
          android:gravity="left"
          android:maxLines="1"
          android:singleLine="true"
          android:textColor="#00a2ff"
          android:textSize="18sp" />

</RelativeLayout>

这篇关于Android使左侧的textview缩短,而右侧保持完全可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 09:44