Android将自定义烤面包放在用户';触摸
我已经为祝酒词创建了自定义背景。我使用MotionEvent抓取rawX和rawY,然后显示用户点击的土司。唯一的问题是,烤面包的顶部出现在用户接触点的下方,而不是上方。所以基本上,如果我在展示时知道烤面包的高度,我可以将它沿Y轴向上移动这么多,这样它就会位于用户点击x,Y点的上方。然而,在吐司被画出来之前,你无法得到它的高度。还有其他方法可以利用重力参数获得正确的位置吗Android将自定义烤面包放在用户';触摸,android,positioning,toast,Android,Positioning,Toast,我已经为祝酒词创建了自定义背景。我使用MotionEvent抓取rawX和rawY,然后显示用户点击的土司。唯一的问题是,烤面包的顶部出现在用户接触点的下方,而不是上方。所以基本上,如果我在展示时知道烤面包的高度,我可以将它沿Y轴向上移动这么多,这样它就会位于用户点击x,Y点的上方。然而,在吐司被画出来之前,你无法得到它的高度。还有其他方法可以利用重力参数获得正确的位置吗 //Retrieve the layout inflator LayoutInflater
//Retrieve the layout inflator
LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//Assign the custom layout to view
//Parameter 1 - Custom layout XML
//Parameter 2 - Custom layout ID present in linearlayout tag of XML
View layout = inflater.inflate(R.layout.popup_toast,
(ViewGroup) llayParent.findViewById(R.id.popToastLayoutRoot));
m_tv_what = (TextView)layout.findViewById(R.id.ptextWhat);
m_tv_priority = (TextView)layout.findViewById(R.id.ptextPrior);
m_tv_where = (TextView)layout.findViewById(R.id.ptextWhere);
m_tv_notes = (TextView)layout.findViewById(R.id.ptextNotes);
m_tv_what.setText(row.sSubject);
switch((int)row.lPriority){
case 3:
m_tv_priority.setText("low");
break;
case 2:
m_tv_priority.setText("medium");
break;
case 1:
m_tv_priority.setText("HIGH");
break;
}
m_tv_where.setText(row.sWhere);
m_tv_notes.setText(row.sDescription);
//Return the application context
Toast toast = new Toast(ctx);
//Set toast gravity to bottom
//the height is 0 of course , because it has not been drawn yet
//any way to get height?
int height =layout.getHeight();
toast.setGravity(Gravity.TOP|Gravity.LEFT ,(int)x, (int)y );
//Set toast duration
toast.setDuration(Toast.LENGTH_LONG);
//Set the custom layout to Toast
toast.setView(layout);
//Display toast
toast.show();
您需要使用setGravity()方法,首先获取用户触摸的坐标,然后尝试以下操作:
toast.setGravity(Gravity.TOP|Gravity.LEFT, x, y);
其中x和y是从左上角开始的x和y坐标中的偏移量,重力是一个常数。我使用了一个具有透明背景的活动,而不是烤面包。活动的内容视图(RelativeLayout)包含我为toast设置的相同视图。然后,我动态设置RelativeLayout的padding\u top,以便将其向下推,使RelativeLayout的底部正好位于用户触摸的位置。我已经在使用SetGravity()查看我的代码…但它将其定位在用户点击点下方,我想将其定位在用户点击点上方,换句话说,烤面包片的底部就在用户tapWell的y位置,只需修改偏移量,这正是问题所在,我不知道偏移量是多少,由于屏幕分辨率不同,它不能是一个静态值——因此它必须是动态的,我需要的是我显示它时烤面包的高度,我不知道如何获得它。