ฉันจะเพิ่มและลบมุมมองจากเค้าโครงได้อย่างไร
ฉันจะเพิ่มและลบมุมมองจากเค้าโครงได้อย่างไร
คำตอบ:
ฉันทำแล้ว:
((ViewManager)entry.getParent()).removeView(entry);
(ViewGroup):)
ใช้ ViewStub และระบุเค้าโครงของมุมมองที่คุณต้องการสลับ ดู:
mViewStub.setVisibility(View.VISIBLE) or mViewStub.inflate();
หายไป:
mViewStub.setVisibility(View.GONE);
วิธีนี้เป็นวิธีที่ดีที่สุด
LinearLayout lp = new LinearLayout(this);
lp.addView(new Button(this));
lp.addView(new ImageButton(this));
// Now remove them
lp.removeViewAt(0); // and so on
หากคุณมีเลย์เอาต์ xml ไม่จำเป็นต้องเพิ่มแบบไดนามิกเพียงแค่โทร
lp.removeViewAt(0);
ในการเพิ่มมุมมองให้กับเค้าโครงคุณสามารถใช้addViewวิธีการของViewGroupคลาส ตัวอย่างเช่น,
TextView view = new TextView(getActivity());
view.setText("Hello World");
ViewGroup Layout = (LinearLayout) getActivity().findViewById(R.id.my_layout);
layout.addView(view);
นอกจากนี้ยังมีวิธีการลบอีกหลายวิธี ตรวจสอบเอกสารของViewGroup วิธีง่ายๆอย่างหนึ่งในการลบมุมมองออกจากเค้าโครงสามารถทำได้เช่น
layout.removeAllViews(); // then you will end up having a clean fresh layout
สำหรับการเปลี่ยนการมองเห็น:
predictbtn.setVisibility(View.INVISIBLE);
สำหรับการลบ:
predictbtn.setVisibility(View.GONE);
ผู้โจมตีที่ยอดเยี่ยมจาก Sameer และ Abel Terefe อย่างไรก็ตามเมื่อคุณลบมุมมองในตัวเลือกของฉันคุณต้องการลบมุมมองที่มีรหัสบางอย่าง นี่คือวิธีที่คุณทำ
1 ให้ดู id เมื่อคุณสร้าง:
_textView.setId(index);
2 ลบมุมมองด้วย id:
removeView(findViewById(index));
คุณสามารถใช้ addView หรือ 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);
ก๊อตลิน:
// 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)
สวัสดีถ้าคุณเป็นคนใหม่ใน Android ให้ใช้วิธีนี้ใช้มุมมองของคุณเพื่อให้หายไปเป็นวิธีหนึ่งอย่างอื่นรับมุมมองของผู้ปกครองและนำเด็กออกจากที่นั่น ..... อื่นรับเค้าโครงหลักและใช้สิ่งนี้ วิธีลบ parentView.remove ลูกทั้งหมด (ลูก)
ฉันขอแนะนำให้ใช้วิธี GONE ...
ฉันกำลังลบมุมมองโดยใช้วิธีเริ่มต้นและนับฉันได้เพิ่มมุมมอง 3 มุมมองในเค้าโครงเชิงเส้น
view.removeViews (0, 3);
เพิ่มส่วนขยายนี้:
myView.removeSelf()
fun View?.removeSelf() {
this ?: return
val parent = parent as? ViewGroup ?: return
parent.removeView(this)
}
นี่คือตัวเลือกบางส่วน:
// 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)
}