Display TextView over ImageView in android(在android中在ImageView上显示TextView)
问题描述
我想在图像视图上显示文本.我像 Alesqui 在这里建议的那样做:Android 文字覆盖图片
I want to display a text over an Image View. I do it like Alesqui suggested here: Android Text over image
Android Studio 中的预览看起来不错:
The preview in Android studio looks fine:
但我的实际结果看起来像这样,上面的文字不需要:
But my actual result looks like this with the text unwantedly above:
我必须在执行期间将以下 XML 动态添加到 LinearLayout:
I have to add the following XML dynamically to a LinearLayout during the execution:
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativelayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <ImageView
        android:id="@+id/myImageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="QGRyYXdhYmxlL215SW1hZ2VTb3VjZQ==" />
    <TextView
        android:id="@+id/myImageViewText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/myImageView"
        android:layout_alignTop="@+id/myImageView"
        android:layout_alignRight="@+id/myImageView"
        android:layout_alignBottom="@+id/myImageView"
        android:layout_margin="1dp"
        android:gravity="center"
        android:text="Hello"
        android:textColor="#000000" />
</RelativeLayout>
我是这样添加的:
LinearLayout ll = (LinearLayout) findViewById(R.id.layout_story_covers);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    for (int i = 0; i < stories.size(); i++)
    {
        // add  imageView
        RelativeLayout coverLayout = (RelativeLayout) inflater.inflate(R.layout.fragment_cover, null);
        ImageView imageView =(ImageView) coverLayout.findViewById(R.id.imageViewCover);
        TextView textView = (TextView) coverLayout.findViewById(R.id.textViewCover);
        textView.setText(stories.get(i).title);
        imageLoader.displayImage(stories.get(i).cover.fileUrl, imageView);
        ll.addView(coverLayout);
    }
这一定与那个父 LinearLayout 有关.获得结果的正确方法是什么?
This must have something to do with that parent LinearLayout. What is the proper way to do get the result?
推荐答案
试试这个:
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativelayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <ImageView
        android:id="@+id/myImageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="QGRyYXdhYmxlL215SW1hZ2VTb3VjZQ==" />
    <TextView
        android:id="@+id/myImageViewText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_margin="1dp"
        android:gravity="center"
        android:text="Hello"
        android:textColor="#000000" />
</RelativeLayout>
                        这篇关于在android中在ImageView上显示TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在android中在ImageView上显示TextView
				
        
 
            
        - 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
 - java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
 - 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
 - 转换 ldap 日期 2022-01-01
 - Eclipse 的最佳 XML 编辑器 2022-01-01
 - 如何指定 CORS 的响应标头? 2022-01-01
 - 未找到/usr/local/lib 中的库 2022-01-01
 - GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
 - 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
 - 获取数字的最后一位 2022-01-01
 
						
						
						
						
						
				
				
				
				