popwindow⾼度_PopupWindow使⽤详解 及标题栏⾼度的获
取及注意事项
Android的对话框有两种:PopupWindow和AlertDialog。它们的不同点在于:AlertDialog的位置固定,⽽PopupWindow的位置可以随意AlertDialog是⾮阻塞线程的,⽽PopupWindow是阻塞线程的PopupWindow的位置按照有⽆偏移分,可以分为偏移和⽆偏移两种;按照参照物的不同,可以分为相对于某个控件(Anchor锚)和相对于⽗控件。具体如下showAsDropDown(View anchor):相对某个控件的位置(正左下⽅),⽆偏移showAsDropDown(View anchor, int xoff, int yoff):相对某个控件的位置,有偏移showAtLocation(View parent, int gravity, int x, int y):相对于⽗控件的位置(例如正中央Gravity.CENTER,下⽅Gravity.BOTTOM等),可以设置偏移或⽆偏移
View Code
l
View Code
PopupWindowDemoActivity.java
package com.tianjf;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class PopupWindowDemoActivity extends Activity implements OnClickListener,
OnCheckedChangeListener {
private Button mbutton01;
private Button mbutton02;
private Button mbutton03;
private Button mbutton04;
private PopupWindow mPopupWindow;
// 屏幕的width
private int mScreenWidth;
// 屏幕的height
private int mScreenHeight;
/
/ PopupWindow的width
private int mPopupWindowWidth;
// PopupWindow的height
private int mPopupWindowHeight;
@Override
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.main);
mbutton01 = (Button) findViewById(R.id.button01);
mbutton02 = (Button) findViewById(R.id.button02);
mbutton03 = (Button) findViewById(R.id.button03);
mbutton04 = (Button) findViewById(R.id.button04);
mbutton01.setOnClickListener(this);
mbutton02.setOnClickListener(this);
mbutton03.setOnClickListener(this);
mbutton04.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
// 相对某个控件的位置(正左下⽅),⽆偏移
case R.id.button01:
getPopupWindowInstance();
mPopupWindow.showAsDropDown(v);
break;
// 相对某个控件的位置(正左下⽅),有偏移
case R.id.button02:
getPopupWindowInstance();
mPopupWindow.showAsDropDown(v, 50, 50);// X、Y⽅向各偏移50 break;
// 相对于⽗控件的位置,⽆偏移
case R.id.button03:
getPopupWindowInstance();
mPopupWindow.showAtLocation(v, Gravity.CENTER, 0, 0);
break;
// 相对于⽗控件的位置,有偏移
case R.id.button04:
getPopupWindowInstance();
mPopupWindow.showAtLocation(v, Gravity.BOTTOM, 0, 50);
break;
default:
break;
}
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
mPopupWindow.dismiss();alertdialog使用方法
}
/*
* 获取PopupWindow实例
*/
private void getPopupWindowInstance() {
if (null != mPopupWindow) {
mPopupWindow.dismiss();
return;
} else {
initPopuptWindow();
}
}
/*
* 创建PopupWindow
*/
private void initPopuptWindow() {
LayoutInflater layoutInflater = LayoutInflater.from(this);
View popupWindow = layoutInflater.inflate(R.layout.popup_window, null);
RadioGroup radioGroup = (RadioGroup) popupWindow.findViewById(R.id.radioGroup);
radioGroup.setOnCheckedChangeListener(this);
/
/ 创建⼀个PopupWindow
// 参数1:contentView 指定PopupWindow的内容
// 参数2:width 指定PopupWindow的width
// 参数3:height 指定PopupWindow的height
mPopupWindow = new PopupWindow(popupWindow, 100, 130);
// 获取屏幕和PopupWindow的width和height
mScreenWidth = getWindowManager().getDefaultDisplay().getWidth();
mScreenWidth = getWindowManager().getDefaultDisplay().getHeight();
mPopupWindowWidth = Width();
mPopupWindowHeight = Height();
}
}
View Code
三、标题栏⾼度的获取
android界⾯分为最上⽅的状态栏、上⽅的标题栏及显⽰正⽂的普通栏
1.获取标题栏⾼度
getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();
2.获取状态栏⾼度
Rect localRect = newRect();
.getDecorView()
.getWindowVisibleDisplayFrame(localRect);
statusHeight= p;
要注意的是,不管是获取状态栏还是获取标题栏的⾼度,都不能在onCreate()⽅法中创建,deBug⼀下就会发现,如果在onCreate()⽅法中创建,会返回空值

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。