【问题标题】:Android: Need Ellipsis for Multiple TextViews in Relative LayoutAndroid:相对布局中的多个文本视图需要省略号
【发布时间】:2014-03-26 06:06:04
【问题描述】:

我是 Android 新手,我有以下任务。

如果文本太长,我想将彼此相邻的三个 TextView 用省略号对齐。目前我正在使用下面的代码来对齐我的 TextView 彼此相邻,但我的问题是如果文本太长,下面的代码没有放置省略号。我有一些技巧可以同时使用here 中的toleftoftorightof,但在我的情况下,它会像here 中提到的那样循环依赖。

<RelativeLayout
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:layout_toRightOf="@+id/reminderText">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/noteTagOne"
                    android:ellipsize="end"
                    android:singleLine="true"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/noteTagTwo"
                    android:ellipsize="end"
                    android:singleLine="true"
                    android:layout_toRightOf="@+id/noteTagOne"
                    android:layout_marginLeft="5sp"
                    />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/noteTagThree"
                    android:layout_toRightOf="@+id/noteTagTwo"
                    android:ellipsize="end"
                    android:singleLine="true"
                    android:layout_marginLeft="5sp"/>
     </RelativeLayout>

我想输出为,

Tag1... Tag2... Tag3...  

基本上,我想要一些想法,无论是在 xml 中我可以实现这一点还是需要以编程方式实现它,因为 TextView 文本是动态的,我无法修复 maxLength,有时可能只存在单个标签,那时它应该采用全文。

注意:我正在 Xamarin 中使用 C# 开发 Android 项目。

添加的代码是针对上面突出显示的部分。我修复了标签图标和文本的重叠。现在只有两个问题。

1) 最后一个标签与右对齐图像重叠。 2)如果TextView文本很短,标签之间会显示间隙

如何解决?

【问题讨论】:

  • 使用LinearLayout并将orientation设置为水平并具有weightSum = "3"。现在在这个LinearLayout 中保留三个TextViews,每个TextView 具有layout_weight="1"layout_width="0dp"...
  • 谢谢,工作得很好,但在这三个 TextView 之前,我有一个 ImageView,如果我添加了,第一个标签不会变成椭圆形...
  • imageview 占用的空间是否应该等于单个 TextView 的空间?
  • 是的,它的大小是 16X16,但是如果我在 LinearLayout 中添加 TextView 意味着图像变得太宽,通过添加它与第一个 TextView 文本重叠。
  • 如果您使用的是RelativeLayout,那么您的LinearLayout 包含TextViews 应该在右侧链接图像并左侧到文件夹图像

标签: android android-relativelayout


【解决方案1】:

试试这个解决方案

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/noteTagOne"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ellipsize="end"
    android:maxWidth="100dp"
    android:singleLine="true"
    android:text="Tag111111111111111111"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/noteTagTwo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="5sp"
    android:layout_toRightOf="@+id/noteTagOne"
    android:ellipsize="end"
    android:maxWidth="100dp"
    android:singleLine="true"
    android:text="Tag2222222222222222"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/noteTagThree"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="5sp"
    android:layout_toRightOf="@+id/noteTagTwo"
    android:ellipsize="end"
    android:maxWidth="100dp"
    android:singleLine="true"
    android:text="Tag3333333333333333"
    android:textAppearance="?android:attr/textAppearanceLarge" />

【讨论】:

  • 它可以工作,但是是否可以在不固定宽度的情况下实现这一点,因为如果文本很小,标签之间会显示间隙,看起来很奇怪......
  • 请告诉我。如果我在聊天笑脸图标椭圆大小不起作用
【解决方案2】:

使用这个 android:ellipsize="marquee" 就可以了

【讨论】:

  • 但是使用 android:ellipsize="marquee",我是否能够在最后得到点(...)?基本上我不希望椭圆形文本可滚动
【解决方案3】:
android:ellipsize="marquee" 

android:ellipsize="end"
android:maxLines="1"
android:scrollHorizontally="true"
android:singleLine="true"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-08
    • 2020-10-21
    相关资源
    最近更新 更多