[英]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_activityName
是EditText
和你用鑄造它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.