gpt4 book ai didi

android - ListView出现在被点击的按钮下方

转载 作者:行者123 更新时间:2023-11-30 01:53:29 25 4
gpt4 key购买 nike

我有以下实现,运行起来没有任何麻烦。当用户单击按钮时,将显示 ListView 。但是, ListView 显示在屏幕中间。我希望它显示在单击按钮的正下方。这是我的实现:

主 Activity

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");
}
}
}

Dialog_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>

最佳答案

这应该有效:

    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);
}
}

通过在 oncreateview 中设置它们来指定 x 和 y 位置。 x 和 y 位置应通过按钮的 x 和 y 位置 + 高度和宽度来计算,以便它显示在按钮上方。

关于android - ListView出现在被点击的按钮下方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32633262/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com