一个TextView内嵌另一个TextView

3

我有类似于 "There are 200€" 的内容,但我想要对值进行样式化(例如,textColor 红色)

有没有办法让两个 textView 看起来像一个完整的textView?

在 textView 中嵌套另一个 textView 或者 textView 容器等。

我可以像这个回答中所示实现它:Change text color of one word in a TextView

但我想从布局文件中实现。有什么机会吗?

谢谢

编辑

<TextView
    android:id="@+id/lastIncome"
    android:text="Tu último ingreso fueron"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="@dimen/dash_font_size"/>
<TextView
    android:id="@+id/lastIncomeValue"
    android:text="200€"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="@dimen/dash_value_font_size"
    android:textColor="@color/greensea"
    android:layout_gravity="right"/>

请发布一些代码。例如,发布文本视图是如何创建和布局的。 - Sumner Evans
只需要两个文本视图。忘掉其他的东西。只想要在同一行上继续使用新的textView并采用另一种样式的简单操作。 - Jorge Chip
请无论如何发布代码。 - Sumner Evans
即使我发布了代码,也没有人提供答案。这是不可能的。也许您更喜欢根据每个屏幕大小更改文本大小,以便将您的TextView并排放置。 - Jorge Chip
如果这仍然是您关心的问题,您可以查看此库:https://github.com/quiqueqs/BabushkaTextSpannable 是我认为您正在寻找的。 - TeePaps
@jorgechip 使用水平方向的线性布局可能会有所帮助。 - Shivam
2个回答

2
您可以使用水平的LinearLayout来实现这一点。LinearLayout是两个并排TextView的容器。这个布局可以放在XML中的任何其他容器(LinearLayout、RelativeLayout等)中。
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="There are " />
    <TextView
        android:id="@+id/amount"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="200€"
        android:textColor="#ff0000"/>
</LinearLayout>

如果"There are"这个词组变长了,比如说变成了"你的收入大约是",并且填满了整个屏幕宽度,那么会发生什么呢?这个值将会出现在屏幕外。 - Jorge Chip
在这种情况下,您可以尝试使用RelativeLayout作为容器,将@+id/amount文本视图与其他TextView对齐以实现“跟随”。 - kiruwka
2
据我理解,TextView 就像是一个块。因此,如果我定义了一个 RelativeLayout,并告诉内联的 @+id/amount 在第一个 TextView 下方或右侧,它会跳过很多空间,因为第一个 TextView 的形状是一个矩形,无论文本何时结束。 - Jorge Chip

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接