在android中,将文本视图放在imageview的顶部 我有一个列表视图,它有一个图像视图,它是 可垂直滚动 我正在尝试将文本视图放置在图像视图 两个视图都必须可见 可能吗 如果是,如何以编程方式进行 我需要做哪些更改

在android中,将文本视图放在imageview的顶部 我有一个列表视图,它有一个图像视图,它是 可垂直滚动 我正在尝试将文本视图放置在图像视图 两个视图都必须可见 可能吗 如果是,如何以编程方式进行 我需要做哪些更改,android,textview,android-imageview,Android,Textview,Android Imageview,列出\u images.xml的\u视图\u项目\u <?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" androi

列出\u images.xml的\u视图\u项目\u

<?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>

它给出如下输出

如何做下面这样的事情


注意:碟片1和碟片2是文本视图

这将为您提供所需的布局:

<?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>

使用android:layout_marginTop=“20dp”查看哪个更适合您。使用id
textview
动态设置
android:text

由于RelativeLayout堆叠其子项,因此在ImageView之后定义TextView会将其置于ImageView之上

注意:使用
FrameLayout
作为父级,可以获得类似的结果,以及使用任何其他android容器的效率增益。感谢Igor Ganapolsky(参见下面的评论)指出这个答案需要更新。

试试这个:

<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"
    />


希望这能对您有所帮助。

只需在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>


这是上面xml的输出,正如您在OP中提到的,您需要以编程方式在
ImageView
上覆盖
Text
。您可以将
ImageView
置于
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);
 }
您可以使用来实现这一点

如何使用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>


参考:你也可以试试这个。我只使用框架布局

 <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>


您必须确保将
图像视图
置于
框架布局中的
文本视图
上方。这很简单,但我希望它能帮助别人。:)

在ImageView上添加文本视图您可以通过将xml编辑为答案来显示它吗。。。。文本视图必须在图像视图顶部可见。秘密是相对的。(而不是线性布局等)我想在imageview上放置一个文本,并希望在任何我想拖放的地方都能自由拖放。类似于“Phonto”应用程序中所做的操作。如果要垂直对齐
,请删除
android:layout\u alignParentTop
android:layout\u marginTop
,并将
android:layou-centerInParent=“true”
添加到
@LeeHanKyeol>中。另一方面,如果只需要对齐,
center\u vertical
android:layout\u centerVertical=“true”
就足够了。为什么这里的RelativeLayout比FrameLayout更受欢迎?@IgorGanapolsky I根据问题中给出的细节(布局)给出了我的答案。我将
RelativeLayout
保存下来,以备极少数情况下完美匹配-除非给出的布局是实际布局的简化版本,否则它不在这里。另外,
FrameLayout
并不像想象中的那样毫无特色。谢谢,答案已经过编辑以反映这一点。我们可以从中创建位图吗?您必须确保将
ImageView
放在
TextView