【问题标题】:Custom design Text Box自定义设计文本框
【发布时间】:2018-09-03 08:24:03
【问题描述】:

我想在文本框上方进行设计,但不知道该怎么做。

我搜索了以上的 png,svg 没有找到任何。

请帮我提个建议?

【问题讨论】:

    标签: android textbox textview android-custom-view


    【解决方案1】:

    解决方案:使用LayerDrawable来实现你的文本框设计。

    简单来说,LayerDrawable 是什么?

    LayerDrawable 是一个可绘制对象,用于管理其他数组 绘图。列表中的每个可绘制对象都按顺序绘制 列表——列表中的最后一个可绘制对象绘制在顶部。

    每个可绘制对象都由单个元素中的一个元素表示 元素。

    从你的文本框设计来看,我会分成4个元素。

    • 顶部水平中心的箭头图标
    • 矩形中的文本框内容
    • 位图中的左上角图像
    • 位图中的右上角

    全部放入background_text_box.xml文件:

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    
        <!-- The arrow in triangle by rotate a rectangle 45 degrees -->
        <item
            android:width="10dp"
            android:height="10dp"
            android:gravity="center_horizontal"
            android:top="0dp">
            <rotate android:fromDegrees="45">
                <shape android:shape="rectangle">
                    <solid android:color="#FFF1A4" />
                </shape>
            </rotate>
    
        </item>
    
        <!-- The content in rectangle -->
        <item
            android:width="300dp"
            android:height="100dp"
            android:gravity="fill"
            android:top="5dp">
            <shape android:shape="rectangle">
                <solid android:color="#FFF1A4" />
            </shape>
        </item>
    
        <!-- The left corner image in bitmap-->
        <item
            android:gravity="top|left"
            android:top="5dp">
            <bitmap android:src="@drawable/icon" />
        </item>
    
        <!-- The right corner image in bitmap -->
        <item
            android:gravity="top|right"
            android:top="5dp">
            <bitmap android:src="@drawable/icon" />
        </item>
    
    </layer-list>
    

    结果如下:

    让我们测试一下:

    activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <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"
        tools:context=".MainActivity">
    
        <TextView
            android:id="@+id/spinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingStart="50dp"
            android:paddingEnd="50dp"
            android:gravity="center"
            android:text="Congratulation...!\nLaxminarayan is top performaer\nof month December 2017"
            android:background="@drawable/background_text_box"/>
    </LinearLayout>
    

    文本框如下所示:

    【讨论】:

    • 谢谢你,这就是我想要的。再次感谢您!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-02
    • 2013-06-29
    • 1970-01-01
    • 1970-01-01
    • 2021-09-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多