【问题标题】:Adding text to a drawable shape将文本添加到可绘制形状
【发布时间】:2014-05-03 16:11:15
【问题描述】:

我有这个drawable:

<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape>
  <solid
      android:color="@android:color/transparent" />
</shape>
</item>

<item>
<shape>
  <gradient
      android:startColor="#8FED85"
      android:endColor="#4CD43D"
      android:angle="270" />
</shape>
</item>
</selector>

并通过以下方式显示:

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


<item android:drawable="@android:color/transparent" android:state_focused="true"   android:state_pressed="false"/>
<item android:drawable="@android:color/transparent" android:state_pressed="true"/>
<item android:drawable="@drawable/listview_selector"/>


 </selector>

这是列表视图:

<ListView
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@+id/imageView9"
    android:divider="#fff"
    android:cacheColorHint="#00000000"
    android:dividerHeight="0.9dp"
    android:listSelector="@drawable/list_selector_animation"
    android:choiceMode="singleChoice"
    android:layout_marginBottom="50dp"
    android:background="#fff"
    >

</ListView>

drawable 是一个绿色的“框”,当列表中的一个项目被选中时,它会显示在列表视图中,我想做的也是在 drawable 中显示一个文本,例如“再次点击发送”或其他内容像这样,关于如何在可绘制形状内添加文本的任何提示?

【问题讨论】:

  • 如果你想给一些drawable添加静态/动态文本,你可以扩展Drawable类并覆盖它的draw方法
  • 你可以把这个drawable放到一个按钮上,作为它的背景,并使用它的setText来添加文本
  • @LenaBru 谢谢,这是个好主意,我会试试的 :)

标签: android android-listview android-animation android-drawable


【解决方案1】:

你可以把这个drawable放在一个按钮上,作为它的背景,并使用它的setText来添加文本

【讨论】: