我试图制作一个可以在app中拖放的按钮。如何在应用程序中拖放按钮?
var layout = new LinearLayout (this);
layout.Orientation = Orientation.Vertical;
var aButton = new Button (this);
aButton.Text = "Say Hello!";
layout.AddView (aButton);
SetContentView (layout);
答案 0 :(得分:0)
我试图制作一个可以在app中拖放的按钮。如何在应用程序中拖放按钮?
您可以按照以下步骤实施拖放操作:
使用AbsoluteLayout
创建您的xaml文件:
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_blue_light">
<Button
android:id="@+id/btnDrapDrop"
android:layout_width="50dp"
android:layout_height="50dp"
android:text="Button To Drag"/>
</AbsoluteLayout>
将按钮的长按事件注册为拖放事件,并在事件中调用View.StartDrag
:
Button btnDrapDrop;
...
btnDrapDrop.LongClick += BtnDrapDrop_LongClick;
...
private void BtnDrapDrop_LongClick(object sender, Android.Views.View.LongClickEventArgs e)
{
ClipData dragData = ClipData.NewPlainText("", "");
View.DragShadowBuilder myShadow = new View.DragShadowBuilder(btnDrapDrop);
btnDrapDrop.StartDrag(dragData, myShadow, null, 0);
}
注册Drag
的{{1}}事件以接收放弃并在用户放弃时翻译按钮:
AbsoluteLayout
以下是完整的活动:
AbsoluteLayout layout;
...
layout.Drag += BtnDrapDrop_Drag;
...
private void BtnDrapDrop_Drag(object sender, View.DragEventArgs e)
{
AbsoluteLayout layout = (AbsoluteLayout)sender;
switch (e.Event.Action)
{
case DragAction.Drop:
float x=e.Event.GetX();
float y = e.Event.GetY();
btnDrapDrop.TranslationX = x;
btnDrapDrop.TranslationY = y;
layout.Invalidate();
return;
}
}
有关Android中Drap和Drop的详细信息,请参阅Drag and Drop。