一、listpop(1)
listpop是一種Android中的控件,用於實現列表的彈出框效果。它可以顯示出一個列表,選項可以點擊並反饋給應用程序。listpop(1)代表的是由點擊一個按鈕或者文本的方式觸發該控件的使用。
在使用listpop(1)時,首先需要在布局文件中聲明一個按鈕或者文本,並設置其點擊事件。在點擊事件裏面,實例化一個Listpop對象,然後設置需要顯示的選項內容,並顯示列表彈出框。下面是一個示例代碼:
Button btn = (Button) findViewById(R.id.button); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Listpop listpop = new Listpop(MainActivity.this); listpop.setItems(new String[]{"選項一", "選項二", "選項三"}); listpop.show(); } });
在這個示例代碼中,我們首先通過findViewById獲得了一個按鈕的實例,並設置其點擊事件。在點擊事件中,我們實例化了一個listpop對象,並通過setItems方法設置了需要顯示的選項內容,最後調用show方法顯示彈出框。
二、listpopupwindow
listpopupwindow是Android中的一個彈出框控件,與listpop的效果類似,但是更加靈活,可以在指定的位置彈出,並且可以自定義彈出框的大小和樣式。使用listpopupwindow需要熟悉其中的一些方法和屬性。
首先需要實例化一個ListPopupWindow對象,然後通過setAdapter方法設置適配器。適配器中的getView方法負責將每一個選項展示出來。ListPopupWindow對象提供了一系列的方法,可以設置以下屬性:
1、setAnchorView:設置彈出框的參考控件。
2、setDropDownGravity:設置彈出框的位置,如Gravity.CENTER等,可以通過計算實現自定義位置。
3、setListSelector:設置選中狀態的背景顏色。
4、setDropDownHeight:設置彈出框的高度。
下面是一個示例代碼:
ListPopupWindow listPopupWindow = new ListPopupWindow(MainActivity.this); listPopupWindow.setAdapter(new ArrayAdapter(MainActivity.this, android.R.layout.simple_spinner_item, new String[]{"選項一", "選項二", "選項三"})); listPopupWindow.setAnchorView(findViewById(R.id.textView)); listPopupWindow.setDropDownGravity(Gravity.START); listPopupWindow.setListSelector(new ColorDrawable(getResources().getColor(R.color.colorAccent))); listPopupWindow.setDropDownHeight(getResources().getDimensionPixelSize(R.dimen.listPopHeight)); listPopupWindow.show();
三、listpopfront
listpopfront是listpopupwindow的一種補充,它可以使彈出框前面顯示一個類似三角形的小圖標。這個小圖標可以增加彈出框的美觀度和可讀性,讓用戶更加容易選擇需要的選項。
使用listpopfront需要自定義一個drawable,並將其設置給listpopupwindow的屬性。listpopfront的drawable由三個部分組成,分別是左邊的三角形、中間的線條和右邊的半圓,需要在XML中定義。
下面是一個listpopfront的示例代碼:
int width = getResources().getDimensionPixelSize(R.dimen.listPopWidth); int height = getResources().getDimensionPixelSize(R.dimen.listPopHeight); ListPopupWindow listPopupWindow = new ListPopupWindow(MainActivity.this); listPopupWindow.setAdapter(new ArrayAdapter(MainActivity.this, android.R.layout.simple_spinner_item, new String[]{"選項一", "選項二", "選項三"})); listPopupWindow.setAnchorView(findViewById(R.id.textView)); listPopupWindow.setWidth(width); listPopupWindow.setHeight(height); Drawable drawable = getResources().getDrawable(R.drawable.listpop_front); listPopupWindow.setBackgroundDrawable(drawable); listPopupWindow.show();
四、listpopupwindow位置
listpopupwindow的位置可以通過setDropDownGravity來設置,但是有時候需要根據其所在的父控件和彈出框的大小來計算位置,以實現更加靈活的布局。下面是一個計算位置的示例代碼:
ListPopupWindow listPopupWindow = new ListPopupWindow(MainActivity.this); listPopupWindow.setAdapter(new ArrayAdapter(MainActivity.this, android.R.layout.simple_spinner_item, new String[]{"選項一", "選項二", "選項三"})); listPopupWindow.setAnchorView(findViewById(R.id.textView)); listPopupWindow.setWidth(getResources().getDimensionPixelSize(R.dimen.listPopWidth)); listPopupWindow.setHeight(getResources().getDimensionPixelSize(R.dimen.listPopHeight)); listPopupWindow.setListSelector(new ColorDrawable(getResources().getColor(R.color.colorAccent))); listPopupWindow.setDropDownGravity(Gravity.START); listPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.WHITE)); listPopupWindow.show(); int[] location = new int[2]; findViewById(R.id.textView).getLocationOnScreen(location); DisplayMetrics dm = getResources().getDisplayMetrics(); int screenHeight = dm.heightPixels; int screenWidth = dm.widthPixels; if (screenHeight - location[1] - getResources().getDimensionPixelSize(R.dimen.listPopHeight) >= 0) { listPopupWindow.setVerticalOffset(location[1] + findViewById(R.id.textView).getHeight()); } else { listPopupWindow.setVerticalOffset(location[1] - getResources().getDimensionPixelSize(R.dimen.listPopHeight)); listPopupWindow.setDropDownGravity(Gravity.START|Gravity.TOP); } int leftMargin = location[0] - (getResources().getDimensionPixelSize(R.dimen.listPopWidth)-findViewById(R.id.textView).getWidth())/2; if (leftMargin screenWidth) { listPopupWindow.setHorizontalOffset(leftMargin + getResources().getDimensionPixelSize(R.dimen.listPopWidth) - screenWidth); }
五、listpopupwindow頂部添加三角形
如果listpopupwindow要求頂部加入一個三角形,可以利用listpopfront實現。先創建一個普通的listpopupwindow,然後在其前面添加一個三角形的drawable即可。
下面是一個示例代碼:
int width = getResources().getDimensionPixelSize(R.dimen.listPopWidth); int height = getResources().getDimensionPixelSize(R.dimen.listPopHeight); ListPopupWindow listPopupWindow = new ListPopupWindow(MainActivity.this); listPopupWindow.setAdapter(new ArrayAdapter(MainActivity.this, android.R.layout.simple_spinner_item, new String[]{"選項一", "選項二", "選項三"})); listPopupWindow.setAnchorView(findViewById(R.id.textView)); listPopupWindow.setWidth(width); listPopupWindow.setHeight(height); listPopupWindow.setDropDownGravity(Gravity.START); listPopupWindow.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.colorWhite))); //添加三角形 Bitmap bitmap = Bitmap.createBitmap(width, height + getResources().getDimensionPixelSize(R.dimen.listPopTriangleHeight), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); Paint paint = new Paint(); paint.setAntiAlias(true); paint.setColor(Color.WHITE); Path path = new Path(); path.moveTo(getResources().getDimensionPixelSize(R.dimen.listPopTriangleLeft), height + getResources().getDimensionPixelSize(R.dimen.listPopTriangleHeight)); path.lineTo(getResources().getDimensionPixelSize(R.dimen.listPopTriangleLeft) + getResources().getDimensionPixelSize(R.dimen.listPopTriangleWidth)/2, height); path.lineTo(getResources().getDimensionPixelSize(R.dimen.listPopTriangleLeft) + getResources().getDimensionPixelSize(R.dimen.listPopTriangleWidth), height + getResources().getDimensionPixelSize(R.dimen.listPopTriangleHeight)); canvas.drawPath(path, paint); NinePatchDrawable ninePatchDrawable = new NinePatchDrawable(getResources(), bitmap, bitmap.getNinePatchChunk(), new Rect(), null); LayerDrawable layerDrawable = new LayerDrawable(new Drawable[]{ninePatchDrawable, listPopupWindow.getBackground()}); layerDrawable.setLayerInset(0, 0, 0, 0, 0); layerDrawable.setLayerInset(1, getResources().getDimensionPixelSize(R.dimen.listPopTriangleWidth)/2, getResources().getDimensionPixelSize(R.dimen.listPopTriangleHeight), getResources().getDimensionPixelSize(R.dimen.listPopTriangleWidth)/2, 0); listPopupWindow.setBackgroundDrawable(layerDrawable); listPopupWindow.show();
六、listpopupwindow替代方案
如果在Android程序中不想使用listpopupwindow,還有其他的替代方案。我們可以自定義一個彈出框控件,將其放置在當前頁面的頂部覆蓋原先的控件,並為其添加需要顯示的選項。下面是一個示例代碼,使用了PopupWindow來實現彈出框:
Button btn = (Button) findViewById(R.id.button); View contentView = LayoutInflater.from(MainActivity.this).inflate(R.layout.listpop, null); PopupWindow popupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true); popupWindow.setFocusable(true); popupWindow.setOutsideTouchable(true); popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.back)); contentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED); int popupWidth = contentView.getMeasuredWidth(); int popupHeight = contentView.getMeasuredHeight(); int[] location = new int[2]; btn.getLocationOnScreen(location); popupWindow.showAtLocation(btn, Gravity.NO_GRAVITY, location[0] - popupWidth / 2 + btn.getWidth() / 2, location[1] - popupHeight);
在這個示例代碼中,我們首先在listpop布局文件中定義了一個LinearLayout,用於顯示選項,然後將其設置為PopupWindow的contentView,在布局文件中可以定義需要的樣式。在代碼中,我們通過PopupWindow的各個屬性來設置彈出框的樣式和位置,並將它在指定的位置彈出。
七、listpopupwindow去掉邊框
在一些特殊的情況下,我們可能需要將listpopupwindow的邊框去掉,以便更好地與界面融合。在listpopupwindow中,我們可以通過setElevation來設置彈出框的陰影,而去掉邊框可以通過setBackgroundDrawable方法來實現。以下代碼演示了如何去掉listpopupwindow的邊框:
int width = getResources().getDimensionPixelSize(R.dimen.listPopWidth); int height = getResources().getDimensionPixelSize(R.dimen.listPopHeight); ListPopupWindow listPopupWindow = new ListPopupWindow(MainActivity.this); listPopupWindow.setAdapter(new ArrayAdapter(MainActivity.this, android.R.layout.simple_spinner_item, new String[]{"選項一", "選項二", "選項三"})); listPopupWindow.setAnchorView(findViewById(R.id.textView)); listPopupWindow.setWidth(width); listPopupWindow.setHeight(height); listPopupWindow.setDropDownGravity(Gravity.START); listPopupWindow.setElevation(getResources().getDimension(R.dimen.listPopElevation)); listPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); listPopupWindow.show();
通過將ColorDrawable的顏色設置為透明,就可以去掉listpopupwindow的邊框了。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/195547.html