我在从相对视图中删除视图时遇到问题。下面的代码以编程方式添加电话字段,然后在下面添加开关。添加字段时,一切都很好,但是我找不到轻松删除它们的方法,即有三个字段,我想删除第二个或第三个,下面的那些字段没有固定的视图。
代码:
private int lastCreatedView;
private int tokensIteration;
private int addButtonId, switch1Id, switch2Id;
private ArrayList<Integer> editTextsIds = new ArrayList<>();
private Contact contact;
private void setPhoneField(String number){
EditText editText = new EditText(new ContextThemeWrapper(this, R.style.ContactDetailsEditText));
editText.setId(View.generateViewId());
editText.setText(number);
editText.setHint(R.string.contact_details_activity_hint_phone_number);
editText.setTextColor(getResources().getColor(android.R.color.white));
editText.setEms(10);
editText.setInputType(InputType.TYPE_CLASS_PHONE);
editText.setImeOptions(tokenizer != null && tokenizer.hasMoreTokens()
? EditorInfo.IME_ACTION_NEXT : EditorInfo.IME_ACTION_DONE);
LinearLayout.LayoutParams editTextParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
editText.setLayoutParams(editTextParams);
editTextsIds.add(editText.getId());
TextInputLayout til = new TextInputLayout(new ContextThemeWrapper(this, R.style.ContactDetailsEditText));
til.setId(View.generateViewId());
RelativeLayout.LayoutParams textInputLayoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
int viewToAttach = lastCreatedView == 0 ? R.id.input_layout_company : lastCreatedView;
textInputLayoutParams.addRule(RelativeLayout.BELOW, viewToAttach);
textInputLayoutParams.addRule(RelativeLayout.END_OF, R.id.contact_photo);
til.setLayoutParams(textInputLayoutParams);
til.setTag(editTextsIds.size());
lastCreatedView = til.getId();
ImageView phoneButton = new ImageView(this);
phoneButton.setImageResource(R.drawable.ic_phone_black_48dp);
phoneButton.setPadding(5,5,5,5);
phoneButton.setScaleType(ImageView.ScaleType.FIT_END);
phoneButton.setColorFilter(Color.argb(255, 255, 255, 255));
RelativeLayout.LayoutParams phoneButtonParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
phoneButtonParams.addRule(RelativeLayout.START_OF, til.getId());
phoneButtonParams.addRule(RelativeLayout.ALIGN_TOP, til.getId());
phoneButtonParams.addRule(RelativeLayout.ALIGN_BOTTOM, til.getId());
phoneButton.setLayoutParams(phoneButtonParams);
phoneButton.setTag(editTextsIds.size());
phoneButton.setOnClickListener(view -> {
Intent call = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + number));
startActivity(call);
});
til.addView(editText);
contactContainer.addView(til);
contactContainer.addView(phoneButton);
ImageView removeNumber = new ImageView(this);
removeNumber.setId(View.generateViewId());
removeNumber.setVisibility(tokensIteration > 0 ? View.VISIBLE : View.GONE);
removeNumber.setImageResource(R.drawable.minus);
RelativeLayout.LayoutParams removeNumberParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
removeNumberParams.addRule(RelativeLayout.END_OF, til.getId());
removeNumberParams.addRule(RelativeLayout.ALIGN_TOP, til.getId());
removeNumberParams.addRule(RelativeLayout.ALIGN_BOTTOM, til.getId());
removeNumber.setLayoutParams(removeNumberParams);
removeNumber.setTag(editTextsIds.size());
removeNumber.setOnClickListener(view -> {
for(View v : getViewsByTag(contactContainer, view.getTag()))
contactContainer.removeView(v);
});
if(tokensIteration > 0) contactContainer.removeView(findViewById(addButtonId));
ImageView addNumber = new ImageView(this);
addNumber.setId(View.generateViewId());
addNumber.setImageResource(R.drawable.plus);
addNumber.setPadding(tokensIteration > 0 ? 5 : 0, 0, 0, 0);
RelativeLayout.LayoutParams addNumberParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
addNumberParams.addRule(RelativeLayout.END_OF, removeNumber.getId());
addNumberParams.addRule(RelativeLayout.ALIGN_TOP, removeNumber.getId());
addNumberParams.addRule(RelativeLayout.ALIGN_BOTTOM, removeNumber.getId());
addNumber.setLayoutParams(addNumberParams);
addNumber.setTag(editTextsIds.size());
addNumber.setOnClickListener(view -> {
//Toast.makeText(this, "Function not available", Toast.LENGTH_SHORT).show();
setPhoneField("");
setSwitches();
}
);
addButtonId = addNumber.getId();
contactContainer.addView(removeNumber);
contactContainer.addView(addNumber);
setSwitches();
}
void setSwitches(){
RelativeLayout.LayoutParams recordCallsParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
recordCallsParams.addRule(RelativeLayout.BELOW, lastCreatedView);
recordCallsParams.addRule(RelativeLayout.ALIGN_START, lastCreatedView);
recordCallsParams.addRule(RelativeLayout.ALIGN_END, lastCreatedView);
if(switch1Id != 0 && switch2Id != 0){
contactContainer.updateViewLayout(findViewById(switch1Id), recordCallsParams);
return;
}
Switch recordCalls = new Switch(this);
recordCalls.setText(R.string.contact_details_activity_record_calls);
recordCalls.setTextColor(getResources().getColor(R.color.white));
recordCalls.setSwitchPadding(5);
recordCalls.setId(View.generateViewId());
recordCalls.setLayoutParams(recordCallsParams);
recordCalls.setChecked(contact != null && contact.getIsRecordCalls());
switch1Id = recordCalls.getId();
Switch switchPrivate = new Switch(this);
switchPrivate.setText(R.string.contact_details_activity_private_contact);
switchPrivate.setTextColor(getResources().getColor(R.color.white));
switchPrivate.setSwitchPadding(5);
switchPrivate.setId(View.generateViewId());
RelativeLayout.LayoutParams switchPrivateParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
switchPrivateParams.addRule(RelativeLayout.BELOW, recordCalls.getId());
switchPrivateParams.addRule(RelativeLayout.ALIGN_START, recordCalls.getId());
switchPrivateParams.addRule(RelativeLayout.ALIGN_END, recordCalls.getId());
switchPrivate.setLayoutParams(switchPrivateParams);
switchPrivate.setChecked(contact != null && contact.getIsPrivateContact());
switch2Id = switchPrivate.getId();
contactContainer.addView(recordCalls);
contactContainer.addView(switchPrivate);
}
private static ArrayList<View> getViewsByTag(ViewGroup root, Object tag){
ArrayList<View> views = new ArrayList<>();
final int childCount = root.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = root.getChildAt(i);
if (child instanceof ViewGroup) {
views.addAll(getViewsByTag((ViewGroup) child, tag));
}
final Object tagObj = child.getTag();
if (tagObj != null && tagObj.equals(tag)) {
views.add(child);
}
}
return views;
答案 0 :(得分:1)
您真的需要完全删除视图吗?如果您只是想隐藏一些视图而不弄乱布局,可以将其设置为INVISIBLE
:
phoneButton.setVisibility(View.INVISIBLE);
BTW:我建议您使用布局资源文件。它更容易使用,特别是当您的布局变得更复杂并且您想要使您的应用程序与多种屏幕尺寸,屏幕方向等兼容时。