在TextView中将图像设置在文本的左侧

时间:2014-04-11 11:45:00

标签: android textview

对于一个noob问题我很抱歉,我认为应该使用Spannable或其他东西,但我不能正确使用它。

我需要TextView中的文本,每行左侧有一个drawable。我该怎么做?

4 个答案:

答案 0 :(得分:2)

试试这个..

使用XML

android:drawableLeft="@drawable/ic_launcher"用于TextView

<TextView
    android:id="@+id/txt"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Button"
    android:drawableLeft="@drawable/ic_launcher" />

使用代码

你可以看到setCompoundDrawablesWithIntrinsicBounds

TextView textView = (TextView) findViewById(R.id.txt);
textView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_launcher, 0, 0, 0);

答案 1 :(得分:1)

在XML文件中的TextView标记中尝试此android:drawableLeft="@drawable/sampleImage"

答案 2 :(得分:0)

请参阅TextView

的此属性
android:drawableLeft="@drawable/yourIcon"

<TextView
  android:id="@+id/TextView1"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:drawableLeft="@drawable/yourIcon"
  android:ems="10"
  android:text="TextView" >

答案 3 :(得分:0)

<TextView
    android:id="@+id/textview1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Your Text"
    android:drawableLeft="@drawable/ic_launcher"
    android:padding="10dip" />

你甚至可以使用一个方向垂直的线性布局,一个ImageView和Textview将textView布局对齐到中心