自定义文本视图

时间:2016-09-20 10:40:15

标签: android textview android-custom-view

我想创建一个这样的标签:

enter image description here

要求是:

  • 矩形应随文字
  • 一起增长
  • 红线的长度取决于属性

我尝试的是为文本背景创建一个9ng图像,并使用padding属性作为红线的长度。通过覆盖onDraw方法绘制红线。

结果是红线是正确的但是背景被扩展,包括像这样的填充:

enter image description here

最好的方法是什么?构建包含标签的自定义视图?以编程方式100%绘制的自定义视图(在这种情况下,调整9ng图像大小的方法是什么?)?

由于 于连

2 个答案:

答案 0 :(得分:1)

尝试这个

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="The text"
            android:textColor="#000000"
            android:background="@drawable/<your_drawable>"
            android:textSize="16sp" />

            <View 
            android:layout_width="0dp"
            android:layout_height="1dp"
            android:background="#f00"
            android:layout_weight="1" />

    </LinearLayout>

希望它能帮到你

答案 1 :(得分:0)

最好的方法是使用两个不同的控制器。

您设置了一个简单的textView,其矩形为背景,宽度设置为wrap-content

在此之后,您可以创建将她定位到所需位置的行(位于控制器右侧或页面末尾或您喜欢的位置),然后根据需要调整大小。

毫无疑问我在这里