我想使用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
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中使用drawableLeft/Right/Bottom/Top来渲染相应位置的图像。
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/image"
android:text="@strings/text"
/>
<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中拥有图标,这使得普通按钮比图像按钮更加灵活。左侧、右侧、顶部等是相对于按钮中的文本的位置关系。你可以在按钮上有多个可绘制物,例如一个在左侧,一个在右侧,中间有文本。
要直接在画布上绘制文本,请按照以下步骤进行:
Create a member Paint object in myImageView
constructor
Paint mTextPaint = new Paint();
Use canvas.drawText
in your myImageView.onDraw()
method:
canvas.drawText("My fancy text", xpos, ypos, mTextPaint);
浏览Paint和Canvas类文档,以添加花哨的效果。
<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>
TextView
,因为这种方法会创建额外的布局,不推荐使用。 - Tautvydas