Xml tarafında şu şekilde sol tarafa icon atayabiliyoruz

android:drawableLeft=""

Bunu nasıl kod tarafında yapabiliriz? Yani ben istediğim zaman atayacağım resmi.

soruldu: 16 Tem '12, 10:13

emax_64's gravatar image

emax_64
64589102101
cevap kabul oranı: 20%


Android geliştirici sitesinde belirtildiğine göre aşağıdaki kod işinizi görmeli.

[android:drawableLeft][1]

[setCompoundDrawablesWithIntrinsicBounds(int, int, int, int)][2]

textView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.resim,0,0,0);

[1]: http://developer.android.com/reference/android/widget/TextView.html#setCompoundDrawablesWithIntrinsicBounds(int, int, int, int)

[2]: http://developer.android.com/reference/android/widget/TextView.html#setCompoundDrawablesWithIntrinsicBounds(int, int, int, int)

permanent link

cevaplandı: 16 Tem '12, 10:28

baran's gravatar image

baran
2.1k81939
cevap kabul oranı: 30%

Teşekürler işimi gördü.

(16 Tem '12, 11:06) emax_64 emax_64's gravatar image

TextView txt = findViewById(R.id.tv);

// resmini nereye koyacaksan onu ayarlıyorsun left,right,bottom,top ya da 0 editTxt.setCompoundDrawablesWithIntrinsicBounds(R.drawable.image, 0, 0, 0);

//ya da txt.setBackgroundResource(R.drawable.image);

Son olarak ImageSpan kullanabilirsin : ImageSpan Android

Kullandığım koddan örnek vereyim:

res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView android:layout_width="fill_parent" android:id="@+id/tv"
        android:bufferType="spannable" android:layout_height="wrap_content"
        android:text="@string/hello" />
    <TextView android:layout_width="fill_parent" android:id="@+id/tvIcon"
        android:layout_height="wrap_content" />
</LinearLayout>

MainActivity

@Override
public void onCreate( Bundle savedInstanceState )
{
    super.onCreate( savedInstanceState );
    setContentView( R.layout.main );
    TextView txt = (TextView)findViewById( R.id.tv );
    Spannable spannable = (Spannable)txt.getText();
    StyleSpan boldSpan = new StyleSpan( Typeface.BOLD );
    spannable.setSpan( boldSpan, 41, 52, Spannable.SPAN_INCLUSIVE_INCLUSIVE );

    TextView txtIcon = (TextView)findViewById( R.id.tvIcon );
    SpannableStringBuilder ssb = new SpannableStringBuilder( "ikon :  " );
    Bitmap ikon = BitmapFactory.decodeResource( getResources(), R.drawable.icon );
    ssb.setSpan( new ImageSpan( ikon ), 16, 17, Spannable.SPAN_INCLUSIVE_INCLUSIVE );   
    txtIcon.setText( ssb, BufferType.SPANNABLE );
}
permanent link

cevaplandı: 18 Tem '12, 00:22

andev's gravatar image

andev
1436
cevap kabul oranı: 11%

Cevabınız
toggle preview

powered by BitNami OSQA