編輯:關於 Android 程式設計
做 Android 介面時,經常會遇到「點一下按鈕,從螢幕底部滑出一排選項」這種互動。與其硬塞一個 Dialog,不如用 PopupWindow:它能懸浮在目前的 Activity 之上,而且內容可以是任何 View 版面配置,自由度遠比現成的對話框高。
下面這份 item_popupwindows.xml 做出一個貼齊畫面底部的白色面板,由上到下依序是「拍照」、「從相冊中選取」、「取消」三個項目,中間用細分隔線隔開。
<LinearLayout
android:id="@+id/ll_popup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#ffffff"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#ff495a" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="55dp"
android:orientation="horizontal" >
<Button
android:id="@+id/item_popupwindows_camera"
android:layout_width="match_parent"
android:layout_height="55dp"
android:background="@drawable/bt_nobgd"
android:textColor="#585858"
android:textSize="18sp"
android:text="拍照" />
</LinearLayout>
<TextView
android:layout_marginLeft="80dp"
android:layout_width="match_parent"
android:layout_height="1px"
android:background="#f2f2f2" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="55dp"
android:orientation="horizontal" >
<Button
android:id="@+id/item_popupwindows_Photo"
android:layout_width="match_parent"
android:layout_height="55dp"
android:background="@drawable/bt_nobgd"
android:textColor="#585858"
android:textSize="18sp"
android:text="從相冊中選取" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#f3f3f3" />
<Button
android:id="@+id/item_popupwindows_cancel"
android:layout_width="match_parent"
android:layout_height="55dp"
android:background="@drawable/bt_nobgd"
android:textColor="#585858"
android:textSize="18sp"
android:text="取消" />
</LinearLayout>
把 PopupWindow 的細節收進自己的內部類別裡,Activity 只要一行 new PopupWindows(this, v) 就能叫出來,後續要加功能也不必污染 Activity 的生命週期方法。
public class MainActivity extends Activity implements OnClickListener {
private TextView t_send, t_ragion;
private Button btn_images;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
init();
}
private void init() {
t_send = (TextView) findViewById(R.id.t_send);
btn_images = (Button) findViewById(R.id.btn_images);
t_ragion = (TextView) findViewById(R.id.t_ragion);
t_send.setOnClickListener(this);
btn_images.setOnClickListener(this);
t_ragion.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_images:
new PopupWindows(MainActivity.this, v);
break;
default:
break;
}
}
public class PopupWindows extends PopupWindow {
public PopupWindows(Context mContext, View parent) {
View view = View.inflate(mContext, R.layout.item_popupwindows, null);
view.startAnimation(AnimationUtils.loadAnimation(mContext, R.anim.fade_ins));
LinearLayout ll_popup = (LinearLayout) view.findViewById(R.id.ll_popup);
ll_popup.startAnimation(AnimationUtils.loadAnimation(mContext, R.anim.push_bottom_in_2));
setWidth(LayoutParams.FILL_PARENT);
setHeight(LayoutParams.FILL_PARENT);
setBackgroundDrawable(new BitmapDrawable());
setFocusable(true);
setOutsideTouchable(true);
setContentView(view);
showAtLocation(parent, Gravity.BOTTOM, 0, 0);
update();
Button bt1 = (Button) view.findViewById(R.id.item_popupwindows_camera);
Button bt2 = (Button) view.findViewById(R.id.item_popupwindows_Photo);
Button bt3 = (Button) view.findViewById(R.id.item_popupwindows_cancel);
bt1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// 指定拍照後照片的存放路徑
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(
new File(Environment.getExternalStorageDirectory(), "imageTemp.jpg")));
startActivityForResult(intent, 2);
dismiss(); // 關閉 Popup 視窗
}
});
bt2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK, null);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
startActivityForResult(intent, 1);
dismiss();
}
});
bt3.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dismiss();
}
});
}
}
}
Gravity.CENTER 就是置中彈窗。
深入 Android 五大佈局物件: from FrameLayout 到 TableLayout
佈局是 Android UI 開發的骨架。Android 提供了五種基礎佈局——FrameLayout(框架佈局)、LinearLayout(線性佈局)、Absolute
Material Design 深入實踐:用自定義 Behavior 打造上滑顯示的返回頂部按鈕
效果預覽完整源碼附於文末。為什麼需要一個「回到頂部」按鈕當頁面內容特別長時,使用者滑到最底部後,若想把畫面拉回頂端往往得反覆滑動,體驗相當折磨。Android 不像 iO
Android 4.1 移植開發中踩過的四個坑與解法
在 Android 4.1 上手機開發的過程中,累積了幾個比較典型的問題,這裡一併記錄。 問題一:只 push odex 到手機,程式無法運行 解法:修改 dalvik
Android 自定義 Toast:換掉系統默認樣式
系統默認的 Toast 只有一行灰底白字,想放個圖標或換個配色就得自定義。做法並不複雜,照著 API 走兩步就好:先寫一個布局,再用 setView() 塞進 Toast