Android PopupWindow顯示位置和顯示大小,androidpopupwindow
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#DA4F30" android:cacheColorHint="#000000" android:orientation="vertical" > <ListView android:id="@+id/lv_dialog" android:layout_width="match_parent" android:layout_height="match_parent" android:cacheColorHint="#00000000" android:scrollbars="none" android:overScrollMode="never"> </ListView></LinearLayout>
package com.example.showpop;import android.app.Activity;import android.content.Context;import android.graphics.drawable.BitmapDrawable;import android.os.Bundle;import android.view.Gravity;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.view.Window;import android.view.WindowManager;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.ArrayAdapter;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.ListView;import android.widget.PopupWindow;public class MainActivity extends Activity {private LinearLayout layout;private ListView listView;private PopupWindow popupWindow;private String title[] = { "1", "2", "3", "4", "5" };private ImageView imageView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.activity_main);imageView=(ImageView) findViewById(R.id.imageview_above_more);imageView.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {showPopupWindow(imageView);}});}public void showPopupWindow(View parent) {//載入布局layout = (LinearLayout) LayoutInflater.from(MainActivity.this).inflate(R.layout.dialog, null);//找到布局的控制項listView = (ListView) layout.findViewById(R.id.lv_dialog);//設定適配器listView.setAdapter(new ArrayAdapter<String>(MainActivity.this,R.layout.text, R.id.tv_text, title));// 執行個體化popupWindowpopupWindow = new PopupWindow(layout, 300,500);//修飾鍵盤是否可以獲得焦點popupWindow.setFocusable(true);//設定popupWindow彈出表單的背景popupWindow.setBackgroundDrawable(new BitmapDrawable(null,""));WindowManager manager=(WindowManager) getSystemService(Context.WINDOW_SERVICE);@SuppressWarnings("deprecation")//擷取xoffint xpos=manager.getDefaultDisplay().getWidth()/2-popupWindow.getWidth()/2;//xoff,yoff基於anchor的左下角進行位移。popupWindow.showAsDropDown(parent,xpos, 0);//監聽listView.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {//關閉popupWindowpopupWindow.dismiss();popupWindow = null;}});}}源碼
android的popupwindow控制項的大小的問題
popUpWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.page2));
設定的是popupwindow(window容器)的背景。
popUpWindow = new PopupWindow(show_popvieView,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); //是將show_popvieView放入容器中,以自適應作為大小,且容器也採用自適應。
綜上,如果你設定大小,導致show_popvieView沾滿整個螢幕,那麼window容器最為底層,設定的背景坑定是看不見的。
建議:背景設定採用設定show_popvieView的背景。如果有多層,可以在內容裡面鑲嵌,最好別直接設定外層popupwindow容器
android布局方式怎適應不同螢幕大小
你可以即時的計算你的popupwindow所佔空間 比如你現在的初始化傳參是300pxX250px 最好即時的dp轉成px
/**
* 將dip或dp值轉換為px值,保證尺寸大小不變
*
* @param dipValue
* @param scale
* (DisplayMetrics類中屬性density)
* @return
*/
public static int dip2px(float dipValue, float scale) {
return (int) (dipValue * scale + 0.5f);
}
public static DisplayMetrics getMetrics(Activity activity) {
DisplayMetrics metrics = new DisplayMetrics();
Display display = activity.getWindowManager().getDefaultDisplay();
display.getMetrics(metrics);
return metrics;
}
再調用的話就是new PopupWindow(findViewById(R.id.mainlayout), dip2px(300,getMetrics(youractivity)), dip2px(250,getMetrics(youractivity)));
還有 如果是在平板上面顯示 這種方法也會失去效果 如果你想在高解析度下 顯示 就得傳更高的值才有相同的顯示效果 下面是判斷是否是平板的方法
private boolean isPad() {
WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
// 螢幕寬度
float screenWidth = display.getWidth();
// 螢幕高度
float screenHeight = display.getHeight();
DisplayMetrics dm = new DisplayMetrics();
display.getMetrics(dm);
double x = Math.pow(dm.widthPixels / dm.xdpi, 2);
double y = Math.pow(dm.heightPixels / dm.ydpi, 2);
// 螢幕尺寸
double screenInches = Math.sqrt(x + y);
// 大於6尺寸則為Pad
if (screenInches >= 6.0) {
return true;
}
return false;
}...餘下全文>>