android popupmenu icon,带有图标的PopupMenu

本文档展示了如何在Android中创建和使用PopUpWindow,包括设置布局、添加点击事件及显示位置。示例代码详细解释了如何创建一个包含排序选项的PopUpWindow,并在按钮点击时显示。通过实例代码,读者可以了解Android UI组件和事件处理的基本用法。
摘要由CSDN通过智能技术生成

否则我将实现它:

创建PopUpWindow布局:

android:id="@+id/llSortChangePopup"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="@drawable/sort_popup_background"

android:orientation="vertical" >

android:id="@+id/tvDistance"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="@string/distance"

android:layout_weight="1.0"

android:layout_marginLeft="20dp"

android:paddingTop="5dp"

android:gravity="center_vertical"

android:textColor="@color/my_darker_gray" />

android:layout_marginLeft="11dp"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@drawable/sort_popup_devider"

android:contentDescription="@drawable/sort_popup_devider"/>

android:id="@+id/tvPriority"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="@string/priority"

android:layout_weight="1.0"

android:layout_marginLeft="20dp"

android:gravity="center_vertical"

android:clickable="true"

android:onClick="popupSortOnClick"

android:textColor="@color/my_black" />

android:layout_marginLeft="11dp"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@drawable/sort_popup_devider"

android:contentDescription="@drawable/sort_popup_devider"/>

android:id="@+id/tvTime"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="@string/time"

android:layout_weight="1.0"

android:layout_marginLeft="20dp"

android:gravity="center_vertical"

android:clickable="true"

android:onClick="popupSortOnClick"

android:textColor="@color/my_black" />

android:layout_marginLeft="11dp"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@drawable/sort_popup_devider"

android:contentDescription="@drawable/sort_popup_devider"/>

android:id="@+id/tvStatus"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="@string/status"

android:layout_weight="1.0"

android:layout_marginLeft="20dp"

android:gravity="center_vertical"

android:textColor="@color/my_black"

android:clickable="true"

android:onClick="popupSortOnClick"

android:paddingBottom="10dp"/>

然后PopUpWindow在您的中创建Activity:

// The method that displays the popup.

private void showStatusPopup(final Activity context, Point p) {

// Inflate the popup_layout.xml

LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.llStatusChangePopup);

LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View layout = layoutInflater.inflate(R.layout.status_popup_layout, null);

// Creating the PopupWindow

changeStatusPopUp = new PopupWindow(context);

changeStatusPopUp.setContentView(layout);

changeStatusPopUp.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);

changeStatusPopUp.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);

changeStatusPopUp.setFocusable(true);

// Some offset to align the popup a bit to the left, and a bit down, relative to button's position.

int OFFSET_X = -20;

int OFFSET_Y = 50;

//Clear the default translucent background

changeStatusPopUp.setBackgroundDrawable(new BitmapDrawable());

// Displaying the popup at the specified location, + offsets.

changeStatusPopUp.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y + OFFSET_Y);

}

最后将其弹出onClick一个按钮或其他任何东西:

imTaskStatusButton.setOnClickListener(new OnClickListener()

{

public void onClick(View v)

{

int[] location = new int[2];

currentRowId = position;

currentRow = v;

// Get the x, y location and store it in the location[] array

// location[0] = x, location[1] = y.

v.getLocationOnScreen(location);

//Initialize the Point with x, and y positions

point = new Point();

point.x = location[0];

point.y = location[1];

showStatusPopup(TasksListActivity.this, point);

}

});

的好例子PopUpWindow:

http://androidresearch.wordpress.com/2012/05/06/how-to-create-popups-in-android/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值