2016-12-01 82 views
2

請耐心等待,因爲我對視圖和佈局的使用很陌生。以編程方式將視圖添加到滾動視圖中的相對佈局

我正在嘗試創建一個應用程序,用戶可以在其中輸入文本字段,按一個按鈕並顯示一個新的文本字段,然後他們可以繼續以這種方式添加文本字段。

我的解決辦法是讓頂層是一個滾動視圖,然後有作爲之內,一個孩子一個相對的觀點,(這樣我就可以編程用的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> 

這裏是我嘗試添加新編輯視圖的代碼。

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

您可以使用listview代替scollview中的相對佈局,並在列表中添加元素並將其設置爲notifyDataSetChanged()。或者使用LinearLayout而不是Relative Layout。 –

回答

3

第一件事查看ID activity_createAccounts_relativeLayout_activityNameEditText並且您正在使用TextView強制將它投射到EditText

和你實際的問題: 您正在使用相同EditText例如可變newAccount和相對佈局重新加入,如果你想加入,你必須初始化的EditText內onclicklistener相對佈局多一個的EditText。 只需在行之前添加一行newAccount= new EditText(context) onklicklistener代碼numAccounts = numAccounts+1;

快樂編碼!

相關問題