Câu trả lời:
Tôi đã làm như vậy:
((ViewManager)entry.getParent()).removeView(entry);
(ViewGroup):)
Sử dụng ViewStub và chỉ định bố cục của dạng xem bạn muốn chuyển đổi. Xem:
mViewStub.setVisibility(View.VISIBLE) or mViewStub.inflate();
Biến mất:
mViewStub.setVisibility(View.GONE);
Đây la cach tôt nhât
LinearLayout lp = new LinearLayout(this);
lp.addView(new Button(this));
lp.addView(new ImageButton(this));
// Now remove them
lp.removeViewAt(0); // and so on
Nếu bạn có bố cục xml thì không cần thêm động. Chỉ cần gọi
lp.removeViewAt(0);
Để thêm dạng xem vào một bố cục, bạn có thể sử dụng addViewphương thức của ViewGrouplớp. Ví dụ,
TextView view = new TextView(getActivity());
view.setText("Hello World");
ViewGroup Layout = (LinearLayout) getActivity().findViewById(R.id.my_layout);
layout.addView(view);
Ngoài ra còn có một số phương pháp loại bỏ. Kiểm tra tài liệu của ViewGroup . Một cách đơn giản để xóa chế độ xem khỏi bố cục có thể như,
layout.removeAllViews(); // then you will end up having a clean fresh layout
Để thay đổi tầm nhìn:
predictbtn.setVisibility(View.INVISIBLE);
Để loại bỏ:
predictbtn.setVisibility(View.GONE);
Nhà cảm xạ cừ khôi của Sameer và Abel Terefe. Tuy nhiên, khi bạn xóa chế độ xem, trong tùy chọn của tôi, bạn muốn xóa chế độ xem có id nhất định. Đây là cách bạn làm điều đó.
1, cung cấp cho chế độ xem một id khi bạn tạo:
_textView.setId(index);
2, loại bỏ chế độ xem với id:
removeView(findViewById(index));
bạn có thể sử dụng addView hoặc removeView
java:
// Root Layout
LinearLayout linearLayout = new LinearLayout(context);
linearLayout.setGravity(Gravity.CENTER);
linearLayout.setOrientation(LinearLayout.VERTICAL);
// TextView
TextView textView = new TextView(context);
textView.setText("Sample");
// Add TextView in LinearLayout
linearLayout.addView(textView);
// Remove TextView from LinearLayout
linearLayout.removeView(textView);
kotlin:
// Root Layout
val linearLayout = LinearLayout(context)
linearLayout.gravity = Gravity.CENTER
linearLayout.orientation = LinearLayout.VERTICAL
// TextView
val textView = TextView(context)
textView.text = "Sample"
// Add TextView in LinearLayout
linearLayout.addView(textView)
// Remove TextView from LinearLayout
linearLayout.removeView(textView)
Xin chào nếu bạn là người mới sử dụng android, hãy sử dụng theo cách này Áp dụng chế độ xem của bạn để biến nó thành GONE, còn cách khác, hãy giữ chế độ xem chính và xóa con khỏi đó ..... người khác lấy bố cục mẹ và sử dụng cái này phương thức an remove all child parentView.remove (child)
Tôi sẽ đề xuất sử dụng phương pháp GONE ...
Tôi đang xóa chế độ xem bằng Phương pháp bắt đầu và đếm, tôi đã thêm 3 chế độ xem trong Bố cục tuyến tính.
view.removeViews (0, 3);
Thêm phần mở rộng này:
myView.removeSelf()
fun View?.removeSelf() {
this ?: return
val parent = parent as? ViewGroup ?: return
parent.removeView(this)
}
Dưới đây là một số tùy chọn:
// Built-in
myViewGroup.addView(myView)
// Null-safe extension
fun ViewGroup?.addView(view: View?) {
this ?: return
view ?: return
addView(view)
}
// Reverse addition
myView.addTo(myViewGroup)
fun View?.addTo(parent: ViewGroup?) {
this ?: return
parent ?: return
parent.addView(this)
}