2

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

我想在运行时做这个

4

4 回答 4

8

我认为最简单的方法是通过 XML 属性 setCompoundDrawables

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 里面有图片

于 2015-07-12T16:31:10.600 回答
3
ImageSpan ispan = new ImageSpan(context, yourresourceid);
text.setSpan(ispan, index, index + strLength, 0);

或者

    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 );
于 2013-06-11T12:29:43.153 回答
0

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

于 2013-06-11T12:33:34.330 回答
-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);
}

}

于 2013-06-11T12:35:32.827 回答