生活随笔
收集整理的這篇文章主要介紹了
android 设置PopupWindow的显示大小
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
今天,簡單講講android 如何設(shè)置彈出的PopupWindow的大小。
之前,我做了一個PopupWindow,里面是一個ExpandableListView,設(shè)置PopupWindow的大小為:
window.setWidth(
WindowManager.LayoutParams.
MATCH_PARENT)
;
window.setHeight(
WindowManager.LayoutParams.
WRAP_CONTENT)
;
發(fā)現(xiàn)無論里面Item的個數(shù)是多少,PopupWindow都會彈出整個界面。可是我只需要PopupWindow占界面的一部分,這個該怎么辦呢?在網(wǎng)上查找了資料,發(fā)現(xiàn)其實也很簡單。這里記錄一下。
window.setWidth(
WindowManager.LayoutParams.
MATCH_PARENT)
;
int height = getWindowManager().getDefaultDisplay().getHeight()
;
window.setHeight(
height *
3 /
4)
;
這里我首先獲取了手機屏幕的高度,然后設(shè)置PopupWindow的高度為手機屏幕高度的3/4,這樣就解決了。
這里我還貼出搜索資料的代碼:
<?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" ><ListViewandroid: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);//設(shè)置適配器listView.setAdapter(new ArrayAdapter<String>(MainActivity.this,R.layout.text, R.id.tv_text, title));// 實例化popupWindowpopupWindow = new PopupWindow(layout, 300,500);//控制鍵盤是否可以獲得焦點popupWindow.setFocusable(true);//設(shè)置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的左下角進(jìn)行偏移。popupWindow.showAsDropDown(parent,xpos, 0);//監(jiān)聽listView.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {//關(guān)閉popupWindowpopupWindow.dismiss();popupWindow = null;}});}
}
android 設(shè)置PopupWindow的顯示大小就講完了。
就這么簡單。
總結(jié)
以上是生活随笔為你收集整理的android 设置PopupWindow的显示大小的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。