編輯:關於 Android 程式設計
本文介紹兩種在 Button 上實現圖文混排的方式。
一、兩種方法
1、使用 android:drawableTop 等屬性,讓文字的上方(或上下左右)顯示圖片。
2、使用 SpannableString 搭配 ImageSpan,實現更靈活的圖文混排。
二、程式碼實例
效果圖:

上圖「一、二、三、四」用的是第一種方式,下方的大按鈕用的是第二種方式。
package fry;
import com.example.buttonDemo1.R;
import android.app.Activity;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ImageSpan;
import android.widget.Button;
public class Activity02 extends Activity {
private Button btn_big;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity02);
btn_big = (Button) findViewById(R.id.btn_big);
// 用 SpannableString 的 ImageSpan 添加左邊圖片
SpannableString spannableString1 = new SpannableString("left");
ImageSpan imageSpan = new ImageSpan(this, BitmapFactory.decodeResource(getResources(), R.drawable.image_left));
spannableString1.setSpan(imageSpan, 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// 用 SpannableString 的 ImageSpan 添加右邊圖片
SpannableString spannableString2 = new SpannableString("right");
ImageSpan imageSpan2 = new ImageSpan(this, BitmapFactory.decodeResource(getResources(), R.drawable.image_right));
spannableString2.setSpan(imageSpan2, 0, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
btn_big.append(spannableString1);
btn_big.append("我的按鈕");
btn_big.append(spannableString2);
}
}
activity02.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<!--android:drawableTop 實現文字上方有圖片-->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="一"
android:drawableTop="@drawable/star" />
<!--android:drawablePadding 實現圖片與文字間有 30dp 間距-->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="二"
android:drawableTop="@drawable/star"
android:drawablePadding="30dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="三"
android:drawableRight="@drawable/star" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="四"
android:drawableLeft="@drawable/star" />
</LinearLayout>
<Button
android:id="@+id/btn_big"
android:layout_width="180dp"
android:layout_height="200dp" />
</LinearLayout>
第一種方式適合簡單的圖文排列,透過 drawableTop/Bottom/Left/Right 與 drawablePadding 就能快速搞定;第二種方式則更靈活,能讓圖片精確地混排在文字之間。可依實際需求挑選。
Android 動態部署(六):在插件 APK 中啟動 BroadcastReceiver 與 ContentProvider
在 Android 動態部署或插件化框架中,如何啟動插件 APK 的四大元件是核心問題之一。相較於 Activity 與 Service,BroadcastReceive
Android 點讚動畫實作:粒子飛散效果
本文示範一個點讚時的粒子飛散動畫:點一下按鈕,一堆小圖示就從愛心位置噴出並向上飄散。核心是自訂的 DivergeViewSecond,用二次貝茲曲線計算每個粒子的路徑。
Android React Native 使用原生 UI 組件(以 WebView 為例)
Android React Native 已經封裝了幾個常用的原生組件,比如 ScrollView 和 TextInput,但並非所有系統原生組件都被封裝了。因此有些時候
Android 漫游記(2)——ELF 可執行檔格式入門
ELF(Executable and Linkable Format)是類 Unix 系統的可執行檔格式,Android 自然也不例外,.so 與 .o 同樣採用這個標準