我想使用
ImageView
以一种奇特的方式显示一些消息。
如何向
ImageView
添加文本?
要将文本添加到您的
ImageView
,您可以执行以下操作:
<RelativeLayout> // Only works inside a RelativeLayout
<ImageView
android:id="@+id/myImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/myImageSouce" />
<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>
您还可以使用 TextView 并在
ImageView
中将背景图像设置为您想要的。 此外,如果您使用 ImageView
作为按钮,您可以将其设置为可点击
这里是
TextView
的一些基本代码,用于显示上面带有文本的图像。
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/your_image"
android:text="your text here" />
在单一视图中使用文本和图像的最佳选择 试试这个:
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableBottom="@drawable/ic_launcher"
android:text="TextView" />
在TextView中使用drawalbeLeft/Right/Bottom/Top在各自的位置渲染图像。
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/image"
android:text="@strings/text"
/>
我知道这个问题已经消失了,但如果其他人偶然发现这个问题,我想让他们知道。这听起来可能是一件不直观的事情,但您可以使用可点击设置为 false 或其他的按钮。这是因为除了文本之外,按钮还允许设置drawableLeft、drawableRight、drawableTop 等。
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/border_box1"
android:drawableLeft="@drawable/ar9_but_desc"
android:padding="20dp"
android:text="@string/ar4_button1"
android:textColor="@color/white"
android:textSize="24sp" />
新信息: 按钮可以在drawableLeft、drawableRight、drawableTop 和drawableBottom 中包含图标。这使得标准按钮比图像按钮更加灵活。左、右、上等是与按钮中文本的关系。您可以在按钮上放置多个可绘制对象,例如左侧一个、右侧一个和中间的文本。
要直接在画布上绘制文本,请执行以下操作:
在
myImageView
构造函数中创建成员Paint对象
Paint mTextPaint = new Paint();
在
canvas.drawText
方法中使用 myImageView.onDraw()
:
canvas.drawText("My fancy text", xpos, ypos, mTextPaint);
探索 Paint 和 Canvas 类文档以添加奇特的效果。
使用 FrameLayout,您可以将文本放置在图像视图之上,框架布局同时包含 imageView 和 textView。
如果这还不够,并且您想要一些更奇特的东西,例如“绘制”文本,则需要在画布上绘制文本 - 示例如下:如何将 RTL 文本(阿拉伯语)绘制到位图上并正确排序?
您可以使用 TextView 来达到相同的目的,但是如果您想将其与 ImageView 一起使用,那么您必须创建一个类并扩展 ImageView,然后使用 onDraw() 方法将文本绘制到画布上。有关更多详细信息,请访问 http://developer.android.com/reference/android/graphics/Canvas.html
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:layout_weight="30" >
<ImageButton
android:id="@+id/imgbtnUploadPendingPods"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="@string/hello_world"
android:src="@drawable/upload_icon" />
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:paddingTop="30dp"
android:text="@string/pendingpods"
android:textAppearance="?android:attr/textAppearanceSmall" />
</FrameLayout>
如果您不想在外部视图中显示或使用外部视图, 您可以使用(内部属性),如下所示
android:tooltipText
定义: 当用户将鼠标悬停在视图上或长按视图时,Android 会显示“tooltipText”属性的值。