gpt4 book ai didi

c# - 从非子布局动态地将 EditText 添加到另一个 LinearLayout

转载 作者:行者123 更新时间:2023-11-30 02:36:51 25 4
gpt4 key购买 nike

我想在屏幕上有两种布局。一个可滚动,另一个应包含一个按钮,该按钮可以向可滚动布局添加内容并且应始终可见。我不确定我是否在朝着正确的方向前进,但到目前为止我已经有了这个并且我的代码以我没有预料到的方式工作。如果我单击 buttonSPAddText,EditText 将出现在与按钮相同的行中。我希望它以另一种布局 linearLayoutSPTextHolder 出现在它们的下方。

这是我的 xaml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:p1="http://schemas.android.com/apk/res/android"
p1:orientation="vertical"
p1:minWidth="25px"
p1:minHeight="25px"
p1:layout_width="match_parent"
p1:layout_height="match_parent"
p1:id="@+id/linearLayoutSPMain">
<LinearLayout
p1:orientation="horizontal"
p1:minWidth="25px"
p1:minHeight="25px"
p1:layout_width="match_parent"
p1:layout_height="51.0dp"
p1:id="@+id/linearLayoutSPButtonHolder"
p1:layout_weight="1">
<Button
p1:text="AddText"
p1:layout_width="wrap_content"
p1:layout_height="match_parent"
p1:id="@+id/buttonSPAddText"
p1:layout_weight="1" />
<Button
p1:text="Do nothing"
p1:layout_width="wrap_content"
p1:layout_height="match_parent"
p1:id="@+id/buttonSPDoNth"
p1:gravity="center_vertical"
p1:layout_weight="1" />
</LinearLayout>
<ScrollView
p1:minWidth="25px"
p1:minHeight="25px"
p1:layout_width="match_parent"
p1:layout_height="wrap_content"
p1:id="@+id/scrollViewSPText"
p1:layout_weight="6">
<LinearLayout
p1:orientation="vertical"
p1:minWidth="25px"
p1:minHeight="25px"
p1:layout_width="match_parent"
p1:layout_height="wrap_content"
p1:id="@+id/linearLayoutSPTextHolder"
p1:scrollbars="horizontal" />
</ScrollView>
</LinearLayout>

和我的 Activity :

namespace TiesaDrasaAndroid
{
[Activity (Label = "SelectPlayersActivity")]
public class SelectPlayersActivity : Activity
{
private int _textBoxId = 1000;
private LinearLayout _layout = null;

protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
SetContentView (Resource.Layout.SelectPlayers);

_layout = (LinearLayout)FindViewById(Resource.Id.linearLayoutSPTextHolder);
_layout.Orientation = Orientation.Vertical;
Button addPl = FindViewById<Button>(Resource.Id.buttonSPAddText);

addPl.Click += delegate {
this.CreateUserTextBox ();
};
}
private void CreateUserTextBox()
{
var textbox = new EditText (this);
textbox.Id = _textBoxId;
textbox.SetWidth (100);
_textBoxId++;
_layout.AddView (textbox);

}
}

最佳答案

这段代码非常适合我:

public class MainActivity extends Activity {
private int _textBoxId = 1000;
private LinearLayout _layout = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
_layout = (LinearLayout) findViewById(R.id.linearLayoutSPTextHolder);
// _layout.setOrientation(LinearLayout.VERTICAL);
Button addPl = (Button) findViewById(R.id.buttonSPAddText);

addPl.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
CreateUserTextBox();
}
});
}

private void CreateUserTextBox() {
LayoutParams lpView = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
EditText textbox = new EditText(this);
textbox.setId(_textBoxId);
textbox.setText(_textBoxId + "");
textbox.setLayoutParams(lpView);
_textBoxId++;
_layout.addView(textbox);

}
}

关于c# - 从非子布局动态地将 EditText 添加到另一个 LinearLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26413237/

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