繁体   English   中英

Android:如何以编程方式删除 XML 文件的所有元素

[英]Android: How to delete all the Elements of a XML-File programmatically

如标题所述,如何删除任何给定 XML 文件的所有元素。 假设我们有以下 XML 文件:example.xml:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:id="@+id/idExample">

<Button
        android:id="@+id/beispiel2"
        android:onClick="onSelectDas"
        android:text="Check"
/>

<Button
        android:id="@+id/beispiel2"
        android:onClick="onSelectDas"
        android:text="Check"
/>

</androidx.constraintlayout.widget.ConstraintLayout>

问题:如何从示例中删除两个按钮元素。xml - 以编程方式?

public static void removeDirectChildren( Node parent )
{
  NodeList childNodes = parent.getChildNodes();
  while ( childNodes.getLength() > 0 )
  {
    parent.removeChild( childNodes.item( 0 ) );
  }
}

你用根元素调用它。

ConstraintLayout上调用removeAllViews()

调用此方法从 ViewGroup 中删除所有子视图。

contraintLayout.removeAllViews()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM