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

时间:2016-12-01 04:19:20

标签: android android-relativelayout

请注意我,因为我不熟悉使用视图和布局。

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

我的解决方案是让顶层成为一个scrollview,然后在其中创建一个相对视图(这样我就可以使用OnClick()监听器以编程方式在我的代码中插入更多editview。

我已经看过并阅读了几篇与相关观点有关的帖子,但似乎仍有一些我遗漏的内容。我试过了

这是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>

以下是我尝试添加新编辑视图的代码。

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

}

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

感谢任何帮助。

1 个答案:

答案 0 :(得分:3)

标识为activity_createAccounts_relativeLayout_activityName的第一件事是[{1}},而您使用EditText投放它,因此错误地将其投放到TextView

对于你的实际问题: 您正在使用具有变量EditText的相同EditText实例并在相对布局中再次添加它,如果您想在相对布局中添加一个EditText,则必须在onclicklistener中初始化EditText。 只需在行newAccount

之前的onclicklistener代码中添加一行newAccount= new EditText(context)

快乐的编码!