简体   繁体   中英

xamarin(Android) view equivalent to windows forms panel

I am trying to create an app where the user clicks somewhere on the screen and a text field will be created on the point of the click. If I were to make a windows forms application, I would use the panel control but I struggle to find something similar in Xamarin Android. Keep in mind that I am using a scrollView. Is there a xamarin view for Android that works similarly to the windows forms panel or something that will get the job done as well? Thanks in advance!

Here is how I solved it. As W00ly suggested, I used a RelativeLayout as the clickable view. What is good about the RelativeLayout is that you can add views to it in an absolute position, which you cannot do with the view alone. I started by modifying the Main.axml layout file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <include
        android:id="@+id/toolbar"
        layout="@layout/mainToolBar" />
    <include
        android:id="@+id/overflow"
        layout="@layout/overflowToolBar" />
    <ScrollView
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/toolbar"
        android:id="@+id/svwMain">
        <RelativeLayout
            android:minWidth="25px"
            android:minHeight="25px"
            android:layout_width="match_parent"
            android:layout_height="3000px"
            android:id="@+id/relMain"
            android:clickable="true" />
    </ScrollView>
</RelativeLayout>

Please ignore the toolbars. The clickable property is very important. In order to track clicks in Android language, you need three things. In the mainActivity file, I placed a reference to the RelativeLayout and called its SetOnTouchListener() method.

relMain = FindViewById<RelativeLayout>(Resource.Id.relMain);
relMain.SetOnTouchListener(this);

For this to work, your mainActivity has to derive from and extend View.IOnTouchListener:

public class MainActivity : Activity, View.IOnTouchListener 

Finally, you need to add the OnTouch() method to track clicks on any views that have called the SetOnTouchListener() method:

public bool OnTouch(View v, MotionEvent e)
{
    bool doubleClick = false;
    switch (e.Action)
    {
        case MotionEventActions.Down:
            saveClickPosition[0] = clickPosition[0];
            saveClickPosition[1] = clickPosition[1];
            clickPosition[0] = e.GetX();
            clickPosition[1] = e.GetY();
            doubleClick = toggleClick(clickPosition[0], clickPosition[1], saveClickPosition[0], saveClickPosition[1]);
            break;
    }
    if (v.GetType() == typeof(TextView) && doubleClick)
    {
        relMain.RemoveView(v);
        return true;
    }
    switch (v.Id)
    {
        case Resource.Id.relMain:
            if (doubleClick)
            {
                relParams = new RelativeLayout.LayoutParams(300, 140);
                relParams.LeftMargin = (int)clickPosition[0];
                relParams.TopMargin = (int)clickPosition[1];
                var tvwTest = new TextView(relMain.Context);
                tvwTest.Text = string.Format("x: {0} y: {1}", clickPosition[0], clickPosition[1]);
                tvwTest.SetOnTouchListener(this);
                relMain.AddView(tvwTest, relParams);
            }                 
            break;
    }
    return true;
}

My OnTouch() method does a lot more than it needs to to fulfill what my application needs to do. I still leave the full method here, as some of the things I do may be helpful to others. Please keep in mind that my method is messy. I am still testing things using it. Here is a short video showing it all working: Video

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM