【问题标题】:TextView inside a TextViewTextView 内的 TextView
【发布时间】:2014-05-09 07:43:30
【问题描述】:

我有类似“有 200 欧元”之类的内容,但我想设置该值的样式(例如 textColor Red)

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

textView 或 textViewContainer 内的 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"/>

【问题讨论】:

  • 请贴一些代码。例如,发布文本视图的创建和布局方式。
  • 只有两个文本视图。忘记其他一切。只想要一个简单的动作,在同一行上用另一种样式的新 textView 继续
  • 请把代码贴出来。
  • 没有人提供答案,即使我发布了代码。这不可能。也许您更愿意为每个屏幕尺寸更改 textSize 以将您的 textView 并排放置。
  • 如果这甚至是你所关心的问题,你可以查看这个库:github.com/quiqueqs/BabushkaText Spannables 是我认为你正在寻找的东西

标签: android layout textview


【解决方案1】:

您可以使用一种简单的方法在一个 textview 中使用 SpanableString Builder Click here to get this approach

【讨论】:

    【解决方案2】:

    您可以使用水平线性布局来实现这一点。 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”比“Your income are about”更长,并且填满了所有的屏幕宽度,会发生什么?该值将出现在外面
    • 在这种情况下,您可以尝试 RelativeLayout 而不是 Linear 作为容器并将 @+id/amount 文本视图对齐以“关注”您的其他 TextView
    • 据我了解,textViews 就像块。因此,如果我定义了一个 RelativeLayout,然后告诉 inline @+id/amount 在第一个 textView 的下方或右侧,它将跳过很多空间,因为第一个 textView 的形状是一个矩形,无论文本何时完成
    猜你喜欢
    • 2011-10-05
    • 2017-11-25
    • 2012-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多