How to place Compound drawable on top left of Text View

余生长醉 提交于 2020-01-14 09:45:53

问题


We can set compound drawables for textview as below using drawableLeft or setCompundDrawables but using this its only possible to place the drawable on left / right / top / bottom.But i want to put drawables only at top-left and top-right of the Textview. How can i do that ?


回答1:


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

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:src="@drawable/ic_launcher" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/imageView1"
    android:text="@string/text" />

This would do it.




回答2:


I think, there's an easier and faster way than have 3 views. You need to prepare proper 9patch for icon and then use FrameLayout. It's even possible to have only single EditText / TextView using the same drawable as their background. More details are described in this answer.



来源:https://stackoverflow.com/questions/14726583/how-to-place-compound-drawable-on-top-left-of-text-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!