[英]How to add imageview inside textview or add textview inside imageview, android?
I was wondering if it is possible to add textview inside imageview or vice-versa. 我想知道是否可以在imageview中添加textview,反之亦然。 Is it possible?
可能吗? If yes then how?
如果是,那怎么样?
I want to make this at runtime 我想在运行时制作
The simplest way I think is to setCompoundDrawables via XML attributes 我认为最简单的方法是通过XML属性设置setCompoundDrawables
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: 结果:
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 );
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);
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.