android popupwindow 自定义样式,自定义Popupwindow并指定显示位置!

直接上代码

activity_popup_window_tooltip_text.xml

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

android:paddingBottom="@dimen/activity_vertical_margin"

tools:context="com.artshell.trainingdemos.test.PopupWindowTooltipTextActivity">

android:id="@+id/anchor_view"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Click me to see tooltip"

android:layout_centerInParent="true"/>

nav_up.xml (drawable)

android:fromDegrees="45"

android:pivotX="-50%"

android:pivotY="80%"

android:toDegrees="45">

android:width="2dp"

android:color="#92e4e5"/>

tooltip_bg.xml (drawable)

android:shape="rectangle">

android:width="2dp"

android:color="#92e4e5"/>

popup_window_tooltip_layout.xml

android:layout_width="match_parent"

android:layout_height="match_parent"

android:gravity="center"

android:orientation="vertical">

android:id="@+id/tooltip_nav_up"

android:layout_width="25dp"

android:layout_height="25dp"

android:background="@drawable/nav_up"/>

android:id="@+id/tooltip_text"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="@drawable/tooltip_bg"

android:gravity="center"

android:padding="10dp"

android:text="Tooltip using PopupWindow :)"

android:textColor="@android:color/white"

android:textSize="20sp"

android:textStyle="bold"/>

Activity代码:

public class PopupWindowTooltipTextActivity extends ActionBarActivity {

private View contentView;

private PopupWindow tipWindow;

private Button anchor_view;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_popup_window_tooltip_text);

anchor_view = (Button) findViewById(R.id.anchor_view);

contentView = getLayoutInflater().inflate(R.layout.popup_window_tooltip_layout, null);

tipWindow = new PopupWindow(this);

tipWindow.setTouchInterceptor(new View.OnTouchListener() {

@Override

public boolean onTouch(View v, MotionEvent event) {

if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {

tipWindow.dismiss();

}

// 这个地方一定要返回false,你可能会说这里已经消费掉事件了啊应该返回true,参看PopupWindow的私有类PopupViewContainer

// 弹出的View都是交由这个类来管理的,因为它对onTouchEvent()这个回调方法已经提供了一些默认实现,当你是返回true,事件就在此被拦截

// 不会再继续派发了,所以onTouchEvent()无法被调用

return false;

}

});

anchor_view.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

tipWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);

tipWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);

tipWindow.setOutsideTouchable(true);

tipWindow.setTouchable(true);

tipWindow.setFocusable(true);

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.easyicon_net);

// 某些博客说,当没有设置Background的时候,无论是触摸其他空白区还是back键都无法dismiss()掉这个PopupWindow

// 但我注释掉这段代码能dismiss(),有可能是个Bug,已经得google到修复

tipWindow.setBackgroundDrawable(new BitmapDrawable(getResources(),bitmap));

tipWindow.setContentView(contentView);

// 计算这个PopupWindow的显示位置

int[] screen_pos = new int[2];

anchor_view.getLocationOnScreen(screen_pos);

Rect anchor_rect = new Rect(screen_pos[0], screen_pos[1], screen_pos[0] + anchor_view.getWidth(), screen_pos[1] + anchor_view.getHeight());

contentView.measure(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

int contentViewWidth = contentView.getMeasuredWidth();

int contentViewHeight = contentView.getMeasuredHeight();

int position_x = anchor_rect.centerX() - (contentViewWidth / 2);

int position_y = anchor_rect.bottom - (anchor_rect.height() / 2);

tipWindow.showAtLocation(anchor_view, Gravity.NO_GRAVITY, position_x, position_y);

}

});

}

}

还要注意一点就是:当你已经dismiss()掉了,想再次显示PopupWindow,那么它的某些属性需要重新设置,不能一处写好多处使用,参看这个dismiss()方法的源码实现。在没有dismiss()之前,你的PopupWindow内容发生变化了,你可以使用它的update()系列方法来更新.......

这里再帖两个参考链接,希望对你有所帮助

http://www.cnblogs.com/mengdd/p/3569127.html 【Android PopupWindow的使用和分析】

http://michaelye1988.iteye.com/blog/1766629 【Popupwindow的使用】

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值