閱讀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滑動翻頁特效