編輯:關於 Android 程式設計
本文整理一個氣泡視窗的實作:一個帶背景插圖的小視窗,裡面有一段說明文字與一個「我知道了」的連結,點擊後關閉。這種效果常用於新手指引或異常提示。

氣泡由一個背景圖加上兩個 TextView 組成:一個放內容,一個放「我知道了」。氣泡的外觀其實就是這個 layout 的背景,因此只要換掉 android:background 就能替換成不同造型的氣泡圖。
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:background="@drawable/pub_pop_bg4"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/bubble_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18dp"
android:textColor="#000" />
<!-- 我知道了 -->
<TextView
android:id="@+id/bubble_btn"
android:gravity="right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/bubble_text"
android:textSize="20dp"
android:textColorHighlight="#2151E0"
android:text=""
android:textColor="#1A75B9" />
</RelativeLayout>
public class TestBubbleActivity extends Activity {
/** 螢幕寬高 */
private static int SCREEN_WIDTH = 0;
private static int SCREEN_HEIGHT = 0;
private TextView tvBubContent = null; // 氣泡內容
private TextView tvKnow = null; // 「我知道了」
private View bubbleView = null; // 氣泡整體
private Dialog bubbleAlert = null; // 承載氣泡的 Dialog
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getDimension();
bubbleView = getLayoutInflater().inflate(R.layout.overlay_pop, null);
tvKnow = (TextView) bubbleView.findViewById(R.id.bubble_btn);
tvKnow.setText(Html.fromHtml("<u>" + "我知道了" + "</u>"));
tvBubContent = (TextView) bubbleView.findViewById(R.id.bubble_text);
tvBubContent.setText("上次程序異常退出,正在傳輸歷史數據...");
tvKnow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bubbleAlert.cancel();
}
});
int tmpWidth = SCREEN_WIDTH / 5 * 3;
int tmpHeight = SCREEN_HEIGHT / 8;
tvKnow.setMinWidth(tmpWidth);
tvBubContent.setMaxWidth(tmpWidth);
// 以自訂樣式建立 Dialog
bubbleAlert = new Dialog(this, R.style.bubble_dialog);
Window win = bubbleAlert.getWindow();
LayoutParams params = win.getAttributes();
params.x = -(SCREEN_WIDTH / 8); // 水平偏移
params.y = -tmpHeight; // 垂直偏移
params.width = tmpWidth;
win.setAttributes(params);
bubbleAlert.setCancelable(false);
bubbleAlert.setContentView(bubbleView);
bubbleAlert.show();
}
/** 取得螢幕尺寸 */
private void getDimension() {
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
SCREEN_WIDTH = dm.widthPixels;
SCREEN_HEIGHT = dm.heightPixels;
}
}
位置全靠 LayoutParams 的 x、y 偏移控制,因此要讓氣泡指向某個按鈕,只要把該按鈕的螢幕座標減掉一半寬度帶入即可。另外 setCancelable(false) 讓使用者必須按下「我知道了」才能關閉,適合強提示;若希望點外部也能關掉,把它改為 true 即可。最後,R.style.bubble_dialog 通常會把視窗背景設為透明並關閉陰影,這是氣泡看起來「浮」在畫面上的關鍵設定。
手機QQ透明頭像怎麼做?兩種方法讓頭像「消失」
透明頭像的效果是聊天列表裡只剩名字、看不到頭像方塊,視覺上乾淨很多。原理並不神秘:一張全透明的 PNG 圖片,上傳之後自然什麼都不顯示。難點在於「去哪裡拿到這張圖」。方法
用 ContentProvider 讀取手機聯絡人的兩種實作
看到不少 App 內建「讀取聯絡人」功能,實際動手做之後發現原理並不複雜。以下示範兩種取用方式:第一種跳轉到系統內建的聯絡人介面挑選,第二種直接查詢後顯示在自己的頁面上。
仿 QQ 上傳頭像(一):自定義裁剪框與蒙版實現
好久沒寫博客,手都生了。最近因工作來到上海,暫且穩定,抓緊時間寫一篇壓壓驚。 這次模仿 QQ 更換頭像的功能,先看未完成版的效果——順便放一張萌萌噠的圖片: 核心
用原生 DrawerLayout 實作 Android 側滑菜單的簡單做法
先看效果 點擊菜單時可以切換圖示,例如點擊 happy,首頁就會變成一張笑臉,而整個實作過程超級簡單。 你只需要使用 Toolbar 與 DrawerLayout 這兩