ListView出现在单击的按钮下

时间:2015-09-17 14:44:34

标签: android xamarin

我有以下实现,没有麻烦。当用户单击按钮时,列表视图显示。但是,列表视图显示在屏幕中间。我希望它显示在单击按钮的下方。这是我的实施:

MainActivity

namespace DialogExample
{
    [Activity (Label = "DialogExample", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        private Button mClickBtn;
        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);

            // Set our view from the "main" layout resource
            SetContentView (Resource.Layout.Main);

            mClickBtn = FindViewById<Button> (Resource.Id.myButton);
            mClickBtn.Click += MClickBtn_Click;
        }

        void MClickBtn_Click (object sender, EventArgs e)
        {

            FragmentTransaction transaction = FragmentManager.BeginTransaction ();
            Dialog_Form dialogList = new Dialog_Form ();
            dialogList.Show (transaction, "Dialog fragment");
        }
    }
}

Diaolog_Form

namespace DialogExample
{
    public class Dialog_Form:DialogFragment
    {
        private ListView myListView;
        private List<string> myList=new List<string> ();
        public Dialog_Form ()
        {
        }

        public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            base.OnCreateView (inflater, container, savedInstanceState);

            myList.Add ("Jason");
            myList.Add ("Kenny");
            myList.Add ("Tedd");

            var view = inflater.Inflate (Resource.Layout.dialog_form, container, false);

            myListView = view.FindViewById<ListView> (Resource.Id.myListView);

            ArrayAdapter adapter = new ArrayAdapter (Activity, Android.Resource.Layout.SimpleListItem1, myList);
            myListView.Adapter = adapter;

            return view;
        }

      public override void OnActivityCreated (Bundle savedInstanceState)
        {
            Dialog.Window.RequestFeature (WindowFeatures.NoTitle);
            base.OnActivityCreated (savedInstanceState);
        }
    }
}

Main.axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Button
        android:id="@+id/myButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Click Here" />
</LinearLayout>

dialog_form.axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:minWidth="25px"
    android:minHeight="25px">
    <ListView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:id="@+id/myListView" />
    <Button
        android:text="Project Download"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/downloadButton" />
</LinearLayout>

1 个答案:

答案 0 :(得分:1)

This should work:

    public class Dialog_Form:DialogFragment
    {
        private ListView myListView;
        private List<string> myList=new List<string> ();
        public Dialog_Form ()
        {
        }

        public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            base.OnCreateView (inflater, container, savedInstanceState);

            myList.Add ("Jason");
            myList.Add ("Kenny");
            myList.Add ("Tedd");

            var view = inflater.Inflate (Resource.Layout.dialog_form, container, false);

            myListView = view.FindViewById<ListView> (Resource.Id.myListView);

            ArrayAdapter adapter = new ArrayAdapter (Activity, Android.Resource.Layout.SimpleListItem1, myList);
            myListView.Adapter = adapter;

//          Dialog.Window.SetGravity (GravityFlags.CenterHorizontal | GravityFlags.Top);
            var layoutParam = Dialog.Window.Attributes;
            layoutParam.Width = ViewGroup.LayoutParams.MatchParent;
            layoutParam.X = 80;
            layoutParam.Y = 200;

            Dialog.Window.Attributes = layoutParam;

            return view;
        }

        public override void OnActivityCreated (Bundle savedInstanceState)
        {
            Dialog.Window.RequestFeature (WindowFeatures.NoTitle);
            base.OnActivityCreated (savedInstanceState);
        }
    }

Specify the x & y position by setting them in the oncreateview. The x & y position should be calculated by the x & y position from your button + the height & width so it will show it above the button.