Centerally aligned text over ImageView in android Linear Layout

送分小仙女□ 提交于 2019-12-13 03:35:33

问题


How to display text on android ImageView with Linear Layout. I found many examples on stack overflow for this purpose but all are for Relative Layout, Frame Layout But I am not getting it in Linear Layout.

I have tried for android:text="My Text" But its not working.

Please suggest me if its possible to do so. If so how can it be done.

 <ImageView
    android:id="@+id/share_button"
    android:layout_width="fill_parent"
    android:layout_height="25dp"
    android:layout_gravity="center"
    android:layout_margin="5dp"
    android:layout_weight="2"
    android:text="text"
    android:background="@drawable/iosbutton" />

This is the Image attached to this ImageView on which I want to display text in the center.


回答1:


Try this

Use this method

public BitmapDrawable writeOnDrawable(int drawableId, String text){

    Bitmap bm = BitmapFactory.decodeResource(getResources(), drawableId).copy(Bitmap.Config.ARGB_8888, true);

    Paint paint = new Paint(); 
    paint.setStyle(Style.FILL);  
    paint.setColor(Color.BLACK); 
    paint.setTextSize(20); 

    Canvas canvas = new Canvas(bm);
    canvas.drawText(text, 0, bm.getHeight()/2, paint);

    return new BitmapDrawable(bm);
}

call like this

ImageView image=(ImageView)findViewById(R.id.imageView1);  
image.setImageDrawable(writeOnDrawable(R.drawable.ic_launcher,"Hello"));



回答2:


Does it have to be an ImageView?

<TextView
    android:id="@+id/share_button"
    android:layout_width="fill_parent"
    android:layout_height="25dp"
    android:layout_gravity="center"
    android:layout_margin="5dp"
    android:layout_weight="2"
    android:text="text"
    android:gravity="center"
    android:background="@drawable/iosbutton" />


来源:https://stackoverflow.com/questions/16121681/centerally-aligned-text-over-imageview-in-android-linear-layout

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