How to add imageview inside textview or add textview inside imageview, android?

半世苍凉 提交于 2019-12-12 18:21:52

问题


I was wondering if it is possible to add textview inside imageview or vice-versa. Is it possible? If yes then how?

I want to make this at runtime


回答1:


The simplest way I think is to setCompoundDrawables via XML attributes

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

Example:

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

Result:




回答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 );



回答3:


You can use setCompoundDrawables.... methods from TextView. There are many versions of this method. Look here.




回答4:


Try to use frame layout

hERE IS THE SAMPLE CODE

         <?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" />  

AND FOR RUNTIME THIS IS SAMPLE

     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);
}

}



来源:https://stackoverflow.com/questions/17044081/how-to-add-imageview-inside-textview-or-add-textview-inside-imageview-android

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