411
技術社區[雲棲]
android 文字疊加顯示在圖片之上控件的實現 -- 充分利用TextView
1.通過定義XML方式實現
<TextView android: android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="19px" android:gravity="center_horizontal" android:text="測試文字" android:drawablePadding="-20px" //設置字體和圖片之間的距離, 這是實現文字疊加顯示在圖片之上的關鍵點~ android:drawableTop="@drawable/ic_launcher" // 設置圖片顯示在文字的上方 />
2.通過動態代碼實現
TextView v = new TextView(this.getApplicationContext()); v.setCompoundDrawablePadding(-20); v.setGravity(Gravity.CENTER_HORIZONTAL); Drawable image = getResources().getDrawable(icons[i]); image.setBounds(0, 0, image.getMinimumWidth(), image.getMinimumHeight());//非常重要,必須設置,否則圖片不會顯示 v.setCompoundDrawables(null,image, null, null); v.setText("測試文字"); v.setTextSize(TypedValue.COMPLEX_UNIT_PX,19);//設置字體大小為19px

最後更新:2017-04-03 14:54:00