阅读883 返回首页    go 阿里云 go 技术社区[云栖]


android怎样将textview置于imageview之上

https://allenshao.iteye.com/blog/867581


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

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

Java代码  收藏代码
  1. <FrameLayout xmlns:android="https://schemas.android.com/apk/res/android"  
  2.     android:layout_width="fill_parent"  
  3.     android:layout_height="fill_parent">  
  4.   
  5.     <ImageView    
  6.         android:layout_width="fill_parent"   
  7.         android:layout_height="fill_parent"   
  8.       
  9.         android:scaleType="center"  
  10.         android:src="@drawable/golden_gate" />  
  11.       
  12.     <TextView  
  13.         android:layout_width="wrap_content"   
  14.         android:layout_height="wrap_content"   
  15.         android:layout_marginBottom="20dip"  
  16.         android:layout_gravity="center_horizontal|bottom"  
  17.   
  18.         android:padding="12dip"  
  19.           
  20.         android:background="#AA000000"  
  21.         android:textColor="#ffffffff"  
  22.           
  23.         android:text="Golden Gate" />  
  24.   
  25. </FrameLayout>  

 

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

 

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

Java代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!--<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. --><merge xmlns:android="https://schemas.android.com/apk/res/android">  
  8.   
  9.     <ImageView    
  10.         android:layout_width="fill_parent"   
  11.         android:layout_height="fill_parent"   
  12.       
  13.         android:scaleType="center"  
  14.         android:src="@drawable/mini" />  
  15.       
  16.     <TextView  
  17.         android:layout_width="wrap_content"   
  18.         android:layout_height="wrap_content"   
  19.         android:layout_gravity="center"  
  20.         android:padding="12dip"  
  21.         android:textSize="20sp"  
  22.         android:textStyle="bold"  
  23.         android:textColor="#ffff0000"  
  24.           
  25.         android:text="Mini-Countryman" />  
  26.   
  27. </merge>  
  28. <!--</LinearLayout>  
  29. -->  

 

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


最后更新:2017-04-02 17:51:24

  上一篇:go 建立自己的TextView和ImageView的组合View类
  下一篇:go Android提高篇之TabHost结合ViewFlipper实现tab滑动翻页特效