gpt4 book ai didi

android - 2 个 TextView,左边带省略号,右边带 nowrap,单行

转载 作者:搜寻专家 更新时间:2023-11-01 08:48:44 24 4
gpt4 key购买 nike

第一次在论坛发帖,希望一切顺利:)

我正在为我所在城市的公共(public)交通开发一个 Android 应用。

Here is what I have

[ |short destination   ||next departure| ]
[ |way too long dest...||next departure| ]

Here is what I want:

[ |short destination||next departure|    ]
[ |way too long dest...||next departure| ]

这是一个更完整的例子:s28.postimg.org/5gejnvfd9/actual2.png

奇怪的彩色背景只是为了轻松识别布局/ TextView 。您也可以忽略棕色线(没关系)。

基本上,我想要目的地 [红色背景] 具有可变长度,在其右侧,我想要第一个出发时间 [绿色背景]。一切都在一条线上。

我需要始终完整显示第一个出发信息 (nowrap)。目的地可以用省略号 (...) 括起来。[可选问题,如何将省略号'...'替换为'.' ?]

这是我迄今为止最好的工作代码:

    <RelativeLayout 
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<TextView
android:id="@+id/txtTitleDestination"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/txtTitleFirstDeparture"
android:background="#FF0000"
android:ellipsize="end"
android:maxLines="1"
android:padding="0dp"
android:scrollHorizontally="true"
android:textSize="18sp"
android:textStyle="bold" />

<TextView
android:id="@+id/txtTitleFirstDeparture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_alignParentRight="true"
android:background="#00FF00"
android:ellipsize="none"
android:maxLines="1"
android:padding="0dp"
android:textSize="18sp"
android:textStyle="bold"
/>

</RelativeLayout>

我试过 TableLayout 和 LinearLayour 而不是 RelativeLayout,但没有成功 :(

知道我该怎么做吗?

提前致谢!

卢卢克斯

[已解决]只需轻轻修改 valbertos 答案:

titleDestination.post(new Runnable() {
@Override
public void run() {
int widthTextViewDeparture = measureTextWidthTextView(titleFirstTime, pContext);
int widthTextViewDestination = titleDestination.getWidth();
int widthTextViewParent = rl_parent.getWidth();
if(widthTextViewDestination + widthTextViewDeparture > widthTextViewParent) {
titleDestination.setWidth(widthTextViewParent - widthTextViewDeparture);
titleDestination.setEllipsize(TruncateAt.END);
titleDestination.setHorizontallyScrolling(true);
}
}
});

仅在必要时设置省略号可正确截断文本。

Before: 
Lingolsheim Thiergaten --> Lingolsheim... [1'23"] 21h23

With the modification:
Lingolsheim Thiergaten --> Lingolsheim Thi... [1'23"] 21h23

再次感谢:)

最佳答案

您不需要以编程方式执行此操作,ConstraintLayout 很神奇!

只需使用这段代码

app:layout_constraintHorizo​​ntal_bias="0" 左对齐 View

app:layout_constrainedWidth="true" 包裹左边的文字

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="wrap_content"
android:padding="8dp">

<TextView
android:id="@+id/leftText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
app:layout_constrainedWidth="true"
app:layout_constraintHorizontal_bias="0"
app:layout_constraintEnd_toStartOf="@id/rightText"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="|short destination|" />

<TextView
android:id="@+id/rightText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/leftText"
app:layout_constraintTop_toTopOf="parent"
tools:text="|next departure|" />

</androidx.constraintlayout.widget.ConstraintLayout>

这是结果

enter image description here enter image description here

关于android - 2 个 TextView,左边带省略号,右边带 nowrap,单行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25701817/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com