android怎样将textview置于imageview之上

本文介绍了在Android开发中实现图文叠加的三种方法:使用FrameLayout将TextView置于ImageView之上;利用Canvas绘制Bitmap和文本;采用merge标签达到类似效果。每种方法都有其适用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目前看来有三种方法可以实现

1. framelayout, a framelayout is used to stack a TextView on top of an ImageView

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ImageView  
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
    
        android:scaleType="center"
        android:src="@drawable/golden_gate" />
    
    <TextView
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginBottom="20dip"
        android:layout_gravity="center_horizontal|bottom"

        android:padding="12dip"
        
        android:background="#AA000000"
        android:textColor="#ffffffff"
        
        android:text="Golden Gate" />

</FrameLayout>

 

2. TextView on Canvas。 和framelayout不同,Canvas不是继承自ViewGroup,它不可以添加child views,所以你需要使用drawBitmap和drawText方法去实现

 

3. 使用merge tag,方法大致和framelayout一样

<?xml version="1.0" encoding="utf-8"?>
<!--<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
--><merge xmlns:android="http://schemas.android.com/apk/res/android">

    <ImageView  
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
    
        android:scaleType="center"
        android:src="@drawable/mini" />
    
    <TextView
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_gravity="center"
        android:padding="12dip"
        android:textSize="20sp"
        android:textStyle="bold"
        android:textColor="#ffff0000"
        
        android:text="Mini-Countryman" />

</merge>
<!--</LinearLayout>
-->

 

具体与framelayout区别参考 http://developer.android.com/resources/articles/layout-tricks-merge.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值