Android PopupWindow顯示位置和顯示大小,androidpopupwindow

來源:互聯網
上載者:User

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;
}...餘下全文>>
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.