简体   繁体   English

在android中的imageview之上放置一个textview

[英]Placing a textview on top of imageview in android

  • I have a listview , that has a single imageview which is scrollable vertically我有一个listview ,它有一个可垂直滚动的imageview listview
  • I am trying to place a textview on top of Imageview我正在尝试在Imageview之上放置一个textview
  • Both the views must be visible两个视图都必须可见

  1. Is it possible ?有可能吗?
  2. If yes, How to do it programmatically ?如果是,如何以编程方式进行?
  3. What changes should i need to make ?我需要做哪些改变?

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 并在将其放在CanvasPaint上的帮助下进行书写。

 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放在FrameLayoutTextView上。 That's simple but I hope it helps somebody.这很简单,但我希望它可以帮助某人。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM