2013-03-25 27 views
3

我可以用XML屬性設置圖像的TextView rightside像如何在右側的TextView編程方式setImage

    <TextView 
        android:id="@+id/txtarrow1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:drawableRight="@drawable/arrow" 
        android:gravity="right" 
        android:onClick="VideoClick" 
        android:padding="5dip" 
        android:textColor="#feb4e7" > 
       </TextView> 

我想設置

android:drawableRight="@drawable/arrow" 

編程 像

txtarrow1.setBackgroundResource(R.drawable.arrow_blue); 

但這一個不起作用

+0

[在一個TextView問題設置DrawableLeft]的可能重複(http://stackoverflow.com/questions/6931900/setting-drawableleft-in-a-textview-問題) – 2013-03-25 07:53:25

+0

添加此「android:drawableRight =」@ drawable/ic_launcher「行在您的XML文本視圖 – 2015-09-02 13:29:21

回答

9

您可以簡單地使用谷歌找到的TextView的Android的Java文檔。 他們有一個巨大的表格顯示每個屬性的程序選項。

android:drawableRight (@compile time) 
setCompoundDrawablesWithIntrinsicBounds(int,int,int,int) (@runtime) 

繪製在文本的右側。

0

您可以使用SpannableString ..

SpannableString text = new SpannableString("text"); 
text.setSpan(new ImageSpan(getActivity(), R.drawable.arrow_blue), 0, 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE); 
txtarrow1.setText(text); 
3

這裏你要在文本視圖親語法上的特定方向上使用

添加圖像

有2個方法:

1>通過圖片可繪製

例如:

Drawable img = getContext().getResources().getDrawable(R.drawable.dogy); 
    txtarrow1.setCompoundDrawablesWithIntrinsicBounds( 
    img,// left 
    null,//top 
    null,// right 
    null//bottom); 

記住這個方法是最普遍使用滿在圖像存儲在一個視圖(文本視圖,按鈕)

2>通過圖片ID

 txtarrow1.setCompoundDrawablesWithIntrinsicBounds( 
     R.drawable.ic_doggy,// left 
     0,//top 
     0,// right 
     0//bottom); 

最可能的是在不同的圖像,這些方法中使用的多個顯示鑑於像(GridView控件)

記得一定的差異這兩個時可繪製不能顯示圖像設置在& ID設置(零)

相關問題