[英]Placing a textview on top of imageview in android
listview
, that has a single imageview
which is scrollable verticallylistview
,它有一个可垂直滚动的imageview
listview
textview
on top of Imageview
Imageview
之上放置一个textview
list_view_item_for_images.xml list_view_item_for_images.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/flag"
android:layout_width="fill_parent"
android:layout_height="250dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:scaleType="fitXY"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
It gives a output like below它提供如下输出
How to do something like below如何做类似下面的事情
note :: Dish 1 & 2 are textviews注意 :: 菜 1 和 2 是文本视图
This should give you the required layout:这应该为您提供所需的布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/flag"
android:layout_width="fill_parent"
android:layout_height="250dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:scaleType="fitXY"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Play with the android:layout_marginTop="20dp"
to see which one suits you better.使用
android:layout_marginTop="20dp"
看看哪个更适合你。 Use the id textview
to dynamically set the android:text
value.使用 id
textview
动态设置android:text
值。
Since a RelativeLayout stacks its children, defining the TextView after ImageView puts it 'over' the ImageView.由于RelativeLayout 堆叠其子项,因此在ImageView 将其置于ImageView 之上之后定义TextView。
NOTE: Similar results can be obtained using a FrameLayout
as the parent, along with the efficiency gain over using any other android container.注意:使用
FrameLayout
作为父级可以获得类似的结果,并且比使用任何其他 android 容器效率更高。 Thanks to Igor Ganapolsky
(see comment below) for pointing out that this answer needs an update.感谢
Igor Ganapolsky
(见下面的评论)指出这个答案需要更新。
Try this:试试这个:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rel_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/ImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src=//source of image />
<TextView
android:id="@+id/ImageViewText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/ImageView"
android:layout_alignTop="@id/ImageView"
android:layout_alignRight="@id/ImageView"
android:layout_alignBottom="@id/ImageView"
android:text=//u r text here
android:gravity="center"
/>
Hope this could help you.希望这可以帮助你。
you can use framelayout to achieve this.您可以使用framelayout来实现这一点。
how to use framelayout如何使用框架布局
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:src="@drawable/ic_launcher"
android:scaleType="fitCenter"
android:layout_height="250px"
android:layout_width="250px"/>
<TextView
android:text="Frame Demo"
android:textSize="30px"
android:textStyle="bold"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:gravity="center"/>
</FrameLayout>
ref: tutorialspoint参考: 教程点
As you mentioned in OP, you need to overlay Text
on ImageView
programmatically way.正如您在 OP 中提到的,您需要以编程方式在
ImageView
上叠加Text
。 You can get ImageView
drawable and write on it with the help of putting it on Canvas
and Paint
.您可以获得
ImageView
drawable 并在将其放在Canvas
和Paint
上的帮助下进行书写。
private BitmapDrawable writeTextOnDrawable(int drawableId, String text)
{
Bitmap bm = BitmapFactory.decodeResource(getResources(), drawableId).copy(Bitmap.Config.ARGB_8888, true);
Typeface tf = Typeface.create("Helvetica", Typeface.BOLD);
Paint paint = new Paint();
paint.setStyle(Style.FILL);
paint.setColor(Color.WHITE);
paint.setTypeface(tf);
paint.setTextAlign(Align.CENTER);
paint.setTextSize(11);
Rect textRect = new Rect();
paint.getTextBounds(text, 0, text.length(), textRect);
Canvas canvas = new Canvas(bm);
canvas.drawText(text, xPos, yPos, paint);
return new BitmapDrawable(getResources(), bm);
}
just drag and drop the TextView over ImageView in eclipse只需将 TextView 拖放到 Eclipse 中的 ImageView 上
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="48dp"
android:layout_marginTop="114dp"
android:src="@drawable/bluehills" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/imageView1"
android:layout_centerVertical="true"
android:layout_marginLeft="85dp"
android:text="TextView" />
</RelativeLayout>
And this the output the above xml这是上面的xml的输出
you can try this too.你也可以试试这个。 I use just framelayout.
我只使用框架布局。
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/cover"
android:gravity="bottom">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Hello !"
android:id="@+id/welcomeTV"
android:textColor="@color/textColor"
android:layout_gravity="left|bottom" />
</FrameLayout>
You must make sure to put the ImageView
over the TextView
in the FrameLayout
.您必须确保将
ImageView
放在FrameLayout
的TextView
上。 That's simple but I hope it helps somebody.这很简单,但我希望它可以帮助某人。 :)
:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.