繁体   English   中英

以编程方式将视图添加到滚动视图内的相对布局

[英]Adding views programatically to a relative layout within a scrollview

请接受我的意见,因为我是使用视图和布局的新手。

我正在尝试创建一个应用程序,用户可以在其中输入文本字段,按下按钮并显示新的文本字段,并且他们可以以此方式继续添加文本字段。

我的解决方案是让顶层成为滚动视图,然后在其中作为一个相对视图(这样,我便可以使用OnClick()侦听器以编程方式在我的代码中插入更多editviews。

我已经阅读并阅读了其他几篇与相对观点有关的文章,但似乎仍然缺少某些内容。 我努力了

这是XML

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_create_accounts"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.nic.mybudget.CreateAccountsActivity">


<RelativeLayout
    android:id="@+id/activity_create_accounts_relativeLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fillViewport="true"
    >

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/activity_name"
        android:inputType="textAutoComplete"
        android:layout_alignParentTop="true"
        android:id="@+id/activity_createAccounts_relativeLayout_activityName"/>


    <Button
        android:text="+"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_below="@+id/activity_createAccounts_relativeLayout_activityName"
        android:id="@+id/activity_create_accounts_relativeLayout_activityName_addButton" />

    <Button
        android:text="Save"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_below="@+id/activity_create_accounts_relativeLayout_activityName_addButton"
        android:layout_centerHorizontal="true"
        android:id="@+id/activity_create_accounts_relativeLayout_activityName_saveButton" />


</RelativeLayout>

这是我尝试添加新editviews的代码。

public class CreateAccountsActivity extends Activity {

static private final String TAG = "MAIN-Activity";
int numAccounts = 0;
int lastAccountID;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_create_accounts);

    final RelativeLayout Relative = (RelativeLayout) findViewById(R.id.activity_create_accounts_relativeLayout);
    final TextView oldAccount = (TextView) findViewById(R.id.activity_createAccounts_relativeLayout_activityName);
    final TextView newAccount = new TextView(this);

    final Button addNewAccountButton = (Button) findViewById(R.id.activity_create_accounts_relativeLayout_activityName_addButton);
    addNewAccountButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Log.i(TAG, "addNewAccountOnClick");
            numAccounts = numAccounts+1;
            int newAccountID = oldAccount.getId() + numAccounts;

            RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
            rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
            newAccount.setLayoutParams(rlp);
            newAccount.setHint("Hint" );
            newAccount.setId(newAccountID);
            Relative.addView(newAccount);

            RelativeLayout.LayoutParams blp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
            blp.addRule(RelativeLayout.BELOW, newAccountID-1);
            addNewAccountButton.setLayoutParams(blp);
        }
    });
}

}

如您所见,我正在尝试(但失败)的是在页面顶部添加新的编辑视图,然后将其他所有内容向下推。 我在这里相对布局有什么问题?

任何帮助表示赞赏。

第一件事,查看ID为activity_createAccounts_relativeLayout_activityNameEditText和你用铸造它TextView ,这样是错投给EditText

newAccount您的实际问题:您正在使用具有变量newAccount相同EditText实例,并在相对布局中再次添加它,如果要在相对布局中再添加一个EditText,则必须在onclicklistener内初始化EditText。 只需在您的onclicklistener代码中添加一行newAccount= new EditText(context) ,然后再添加numAccounts = numAccounts+1;numAccounts = numAccounts+1;

编码愉快!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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