मैं किसी लेआउट से दृश्य कैसे जोड़ या हटा सकता हूं?
मैं किसी लेआउट से दृश्य कैसे जोड़ या हटा सकता हूं?
जवाबों:
मैंने ऐसा किया है:
((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);
समीर और हाबिल तेरेफ़ से महान अन्वेषक। हालाँकि, जब आप किसी दृश्य को हटाते हैं, तो मेरे विकल्प में, आप कुछ विशेष आईडी के साथ एक दृश्य हटाना चाहते हैं। यहाँ है कि तुम कैसे करते हो।
1, जब आप इसे बनाते हैं, तो उसे एक आईडी दें:
_textView.setId(index);
2, आईडी के साथ दृश्य को हटा दें:
removeView(findViewById(index));
आप addView या removeView का उपयोग कर सकते हैं
जावा:
// 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)
hi यदि आप android में नए हैं तो इस तरह से अपना प्रयोग करें इसे चला गया बनाने के लिए अपना दृष्टिकोण लागू करें, एक और तरीका है, माता-पिता के दृष्टिकोण को पकड़ें, और बच्चे को वहाँ से हटा दें ..... और अभिभावक लेआउट प्राप्त करें और इसका उपयोग करें पद्धति सभी बच्चे को हटाएं
मेरा सुझाव है कि 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)
}