如何在textview中添加imageview或在imageview,android中添加textview?

时间:2013-06-11 12:27:09

标签: android textview imageview

我想知道是否可以在imageview中添加textview,反之亦然。 可能吗? 如果是,那么如何?

我想在运行时制作

4 个答案:

答案 0 :(得分:6)

我认为最简单的方法是通过XML属性设置CompoundDrawables

android:drawableLeft
android:drawableTop
android:drawableRight
android:drawableBottom.

示例:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:drawableRight="@drawable/ic_navigation_chevron_right"
    android:text="Next "
    android:textSize="20dp" />

结果:

textView with image inside

答案 1 :(得分:2)

ImageSpan ispan = new ImageSpan(context, yourresourceid);
text.setSpan(ispan, index, index + strLength, 0);

OR

    TextView textView = (TextView)findViewById( R.id.TextView );
    Spannable spannable = (Spannable)textView.getText();
    StyleSpan boldSpan = new StyleSpan( Typeface.BOLD );
    spannable.setSpan( boldSpan, 41, 52, Spannable.SPAN_INCLUSIVE_INCLUSIVE );

    TextView textView2 = (TextView)findViewById( R.id.TextView2 );
    SpannableStringBuilder ssb = new SpannableStringBuilder( "Here's a smiley  " );
    Bitmap smiley = BitmapFactory.decodeResource( getResources(), R.drawable.emoticon );
    ssb.setSpan( new ImageSpan( smiley ), 16, 17, Spannable.SPAN_INCLUSIVE_INCLUSIVE ); 
    textView2.setText( ssb, BufferType.SPANNABLE );

答案 2 :(得分:0)

您可以使用TextView中的setCompoundDrawables ....方法。这种方法有很多版本。看here

答案 3 :(得分:-1)

尝试使用框架布局

是示例代码

         <?xml version="1.0" encoding="utf-8"?>
  <LinearLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
       android:layout_height="match_parent"
        android:background="@drawable/day">

    <FrameLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
         >
  <ImageView  
    android:id="@+id/ImageView01"  
    android:layout_height="fill_parent"  
    android:layout_width="fill_parent"  
    android:src="@drawable/pinkrose"  


 >
 </ImageView>  
<TextView  
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content"  
     android:textColor="#ff00ff00"
    android:textSize="40dp"  
    android:text="@string/top_text" />  
<TextView  
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content"  
    android:text="@string/bottom_text"  
    android:layout_gravity="bottom"  
    android:gravity="right"  
    android:textColor="#ff00ff00"
    android:textSize="50dp" />  

对于运行时间这是样本

     public class FrameExample extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    LinearLayout ll=new LinearLayout(this);
     ll.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
             LayoutParams.MATCH_PARENT));
     ll.setOrientation(LinearLayout.VERTICAL);
        ll.setBackgroundResource(R.drawable.bunch);
        ll.setGravity(LinearLayout.VERTICAL);
    FrameLayout fl=new FrameLayout(this);

    fl.setLayoutParams(new FrameLayout.LayoutParams
                        (FrameLayout.LayoutParams.MATCH_PARENT,
                        FrameLayout.LayoutParams.MATCH_PARENT));

    ImageView iv=new ImageView(this);
    iv.setLayoutParams(new FrameLayout.LayoutParams
                        (FrameLayout.LayoutParams.MATCH_PARENT,
                       FrameLayout.LayoutParams.MATCH_PARENT));

    iv.setPadding(30, 30, 30, 30);
    iv.setImageResource(R.drawable.pinkrose);
    fl.addView(iv);
    TextView tv1=new TextView(this);
    FrameLayout.LayoutParams params= new FrameLayout.LayoutParams
    ( LayoutParams.MATCH_PARENT,
            LayoutParams.MATCH_PARENT);

    tv1.setGravity(Gravity.TOP);
    tv1.setText("this is upper part of image");
    tv1.setTextColor(0xffff0000);
    tv1.setTextSize(40);
    tv1.setLayoutParams(params);
    fl.addView(tv1);
    TextView tv2=new TextView(this);
    FrameLayout.LayoutParams params1= new FrameLayout.LayoutParams
     ( LayoutParams.MATCH_PARENT,
            LayoutParams.MATCH_PARENT);

    tv2.setLayoutParams(params1);

    tv2.setGravity(android.view.Gravity.BOTTOM);
    tv2.setText("this is lower part of image");
    tv2.setTextColor(0xff00ff00);
    tv2.setTextSize(40);
    fl.addView(tv2);


    ll.addView(fl);
    setContentView(ll);
}

}