編輯:關於 Android 程式設計
AlertDialog dialog = builder.setTitle("消息列表")
.setView(layout)
.create();
dialog.show();
// 設定視窗的大小
dialog.getWindow().setLayout(300, 200);
順序很關鍵:dialog.show() 一定要放在 dialog.getWindow().setLayout(300, 200) 之前,否則不生效。原因是 getWindow() 在 show() 之後才會真正綁定到 WindowManager,未顯示前調整無異於空作。
另一種寫法是直接操作屬性:
WindowManager.LayoutParams params = dialog.getWindow().getAttributes(); params.width = 300; params.height = 200; dialog.getWindow().setAttributes(params);
其實 setLayout 就是對這個方法的封裝:
final WindowManager.LayoutParams attrs = getAttributes();
attrs.width = width;
attrs.height = height;
if (mCallback != null) {
mCallback.onWindowAttributesChanged(attrs);
}
透過 WindowManager 直接把 View 加上去即可:
mWindowManager = getWindowManager(); LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); mDialogView = inflater.inflate(R.layout.dialogview, null); WindowManager.LayoutParams params = new WindowManager.LayoutParams(); params.height = WindowManager.LayoutParams.WRAP_CONTENT; params.width = WindowManager.LayoutParams.WRAP_CONTENT; params.y += 100; params.x += -30; initDialogComponents(mDialogView); // 添加對話框 mWindowManager.addView(mDialogView, params);
同樣是覆寫 onTouchEvent,透過 ACTION_DOWN 記錄起點、ACTION_MOVE 計算位移並重新 layout,同時夾住邊界避免拖出螢幕:
package ui;
import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.widget.Button;
public class MenuButton extends Button {
private Context mContext;
private WindowManager mWm;
int lastX;
int lastY;
int screenWidth;
int screenHeight;
public MenuButton(Context context, AttributeSet attrs) {
super(context, attrs);
this.mContext = context;
mWm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics dm = mContext.getResources().getDisplayMetrics();
screenWidth = dm.widthPixels;
screenHeight = dm.heightPixels;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
measure(0, 0);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
lastX = (int) event.getRawX();
lastY = (int) event.getRawY();
break;
case MotionEvent.ACTION_MOVE:
int dx = (int) event.getRawX() - lastX;
int dy = (int) event.getRawY() - lastY;
int left = this.getLeft() + dx;
int top = this.getTop() + dy;
int right = this.getRight() + dx;
int bottom = this.getBottom() + dy;
if (left < 0) { left = 0; right = left + this.getWidth(); }
if (right > screenWidth) { right = screenWidth; left = right - this.getWidth(); }
if (top < 0) { top = 0; bottom = top + this.getHeight(); }
if (bottom > screenHeight) { bottom = screenHeight; top = bottom - this.getHeight(); }
this.layout(left, top, right, bottom);
lastX = (int) event.getRawX();
lastY = (int) event.getRawY();
break;
case MotionEvent.ACTION_UP:
Animation alphaAnimation = new AlphaAnimation(0.5f, 1f);
alphaAnimation.setDuration(1000);
Animation scaleAnimation1 = new ScaleAnimation(1.0f, 1.2f, 1.0f, 1.2f);
scaleAnimation1.setDuration(500);
Animation scaleAnimation2 = new ScaleAnimation(1.0f, 0.5f, 1.0f, 0.5f);
scaleAnimation2.setDuration(500);
AnimationSet animationSet = new AnimationSet(true);
animationSet.addAnimation(alphaAnimation);
animationSet.addAnimation(scaleAnimation1);
animationSet.addAnimation(scaleAnimation2);
animationSet.setFillAfter(true);
Animation currentAnima = getAnimation();
if (currentAnima != null) {
currentAnima.cancel();
animationSet.reset();
}
startAnimation(animationSet);
break;
}
return true;
}
}
imageView.setScaleType(ImageView.ScaleType.FIT_START); imageView.setAdjustViewBounds(true);
兩行搭配使用即可:setScaleType 決定圖片對齊起始邊,setAdjustViewBounds 則讓 ImageView 依圖片實際比例收縮自身邊界,消除空白。順帶一提,取得螢幕尺寸時 dm.widthPixels 並不含狀態列與導覽列,若拖曳邊界需要更精確,建議搭配 getWindowManager().getDefaultDisplay().getMetrics() 並自行扣除系統 UI 高度。
Android Touch 系統(二):實例詳解 onInterceptTouchEvent 與 onTouchEvent 的調用過程
上一篇文章講述了 Android TouchEvent 的分發過程,其中有兩個重要函數:onInterceptTouchEvent 和 onTouchEvent,它們都可
Android TextView 兩端對齊:原理拆解與自定義實作
Android 的 TextView 預設只提供靠左對齊,做不到排版雜誌那種左右都齊平的效果。原因之一在於系統的行內斷行規則:數字與英文字母屬於半角字元,預設不允許出現在
Android 佈局優化實戰:用 merge 標籤砍掉多餘的 View 層級
稍前的文章已經談過如何借助 <include /> 標籤來重用與共享佈局程式碼。本篇接著說明 <merge /> 標籤的用法,並示範它與 <
詳解 Android Scroller 與 computeScroll 的呼叫機制關係
Android ViewGroup 中的 Scroller 與 computeScroll 之間是什麼關係? 答案:兩者沒有直接的關係。 知道了答案,是不是覺得下文沒必要