android - 带有图标的自定义 TextView

标签 android layout textview icons drawable

我需要获取一些带有自定义图标的 TextView,比如这些:

enter image description here

但我只能这样做:

enter image description here

我的 3 TextView:

        <LinearLayout android:id="@+id/info" android:layout_marginTop="80dp"
            android:layout_width="match_parent" android:layout_height="match_parent"
            android:layout_centerHorizontal="true"
            android:orientation="horizontal"
            android:layout_marginEnd="16dp"
            android:layout_marginRight="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginStart="16dp">

            <TextView android:id="@+id/info_totali" android:clickable="true"
                android:paddingLeft="10dp" android:paddingRight="10dp"
                android:paddingTop="8dp" android:paddingBottom="8dp"
                android:layout_width="0dp" android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/custom_textview_drawable"
                android:gravity="center"
                android:text="Tour totali\n 21"/>

            <TextView android:id="@+id/info_prenotabili" android:clickable="true" android:layout_toRightOf="@+id/tour1"
                android:paddingLeft="10dp" android:paddingRight="10dp"
                android:paddingTop="8dp" android:paddingBottom="8dp"
                android:layout_width="0dp" android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/custom_textview_drawable"
                android:gravity="center"
                android:text="Prenotabili\n 16"
                android:layout_marginLeft="16dp"
                android:layout_marginStart="16dp" />

            <TextView android:id="@+id/info_preferiti" android:clickable="true" android:layout_toRightOf="@+id/tour2"
                android:paddingLeft="10dp" android:paddingRight="10dp"
                android:paddingTop="8dp" android:paddingBottom="8dp"
                android:layout_width="0dp" android:layout_height="wrap_content"
                android:background="@drawable/custom_textview_drawable"
                android:gravity="center"
                android:layout_weight="1"
                android:text="Preferiti\n 3"
                android:layout_marginLeft="16dp"
                android:layout_marginStart="16dp"/>
        </LinearLayout>

custom_textview_drawable:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item>
        <layer-list>
            <item>
                <shape android:shape="rectangle">
                    <!--<solid android:background="?android:attr/selectableItemBackground"/>-->
                    <!--android:color="#ffffff" -->
                    <solid android:color="@color/account_grey_100" />
                    <stroke android:width="1dip"
                        android:color="@color/account_grey_400"/>
                    <corners android:radius="4dp"/> <!--android:bottomRightRadius="7dp"--><!--android:bottomLeftRadius="7dp"--><!--android:topLeftRadius="7dp"--><!--android:topRightRadius="7dp"/>-->
                </shape>
            </item>

            <item>
                <bitmap android:gravity="top" android:src="@mipmap/ic_events"/>
            </item>
        </layer-list>
    </item>

</selector>

最佳答案

使用 android:drawableTop 将图标放在文本上方,使用 android:background 仅绘制灰色矩形。 Here你可以找到文档

关于android - 带有图标的自定义 TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30967565/

相关文章:

android - 获取体系结构类型 (ABI) 到 Android NDK 的 C 预处理器

java - 如何使用查询获取最高行号?

html - Spring中的等效布局(asp.net core)是什么?

Android - TextView 文本从其首字母居中

android - SQLite 数据库的加载程序,无 ContentProvider

c# - Xamarin - TabbedPage 导航错误(页面不能有 parent )

html - 为什么标题、容器和导航栏 div 位于 ul div 之上?

带有 Activity 按钮的Android Listview,可更改行中的文本

android - 使用 TextView 显示 ListView 中项目的附加信息

android - ResourceNotFoundException - 字符串资源 ID