Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在另一个线性布局之后添加线性布局?_Java_Android_Add_Android Linearlayout_Showdialog - Fatal编程技术网

Java 如何在另一个线性布局之后添加线性布局?

Java 如何在另一个线性布局之后添加线性布局?,java,android,add,android-linearlayout,showdialog,Java,Android,Add,Android Linearlayout,Showdialog,我创建了这个布局: <!-- create_structure.xml --> <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/ly_create_structure" android:layout_width="match_parent"

我创建了这个布局:

<!-- create_structure.xml -->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ly_create_structure"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
</LinearLayout>
使用这段代码,我可以添加一个LinearLayout作为最后一个,但我想在LinearLayout之后添加它,该LinearLayout包含单击以显示AlertDialog的按钮。你能帮我实现这个吗

更新1: 我不明白如何得到索引。我试着这样做:

private void addLevel() {
        LinearLayout mainActivityLayout = (LinearLayout)findViewById(R.id.ly_create_structure);
        LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        LinearLayout ly = (LinearLayout) li.inflate(R.create_structure, null);

        ImageButton ib = (ImageButton) ly.findViewById(R.id.add_level);        

       /**
         * New code to get index
         */
        ViewGroup vg = (ViewGroup) ib.getParent();
        int index = mainActivityLayout.indexOfChild(vg);


        ib.setOnClickListener( new View.OnClickListener() {         
            @Override
            public void onClick(View v) {
                showDialog();

            }
        });

        mainActivityLayout.addView(ly, index);
    }
更新2: 我解决了这个问题。我正在showDialog()中移动下面的代码

您可以在以下位置使用方法:

它还获取参数中的视图索引

private void addLevel(int index) {
        LinearLayout mainActivityLayout = (LinearLayout)findViewById(R.id.ly_create_structure);
        LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        LinearLayout ly = (LinearLayout) li.inflate(R.create_structure, null);

        ImageButton ib = (ImageButton) ly.findViewById(R.id.add_level);        

        ib.setOnClickListener( new View.OnClickListener() {         
            @Override
            public void onClick(View v) {
                showDialog();

            }
        });

        mainActivityLayout.addView(ly, index);
    }
好,,
首先,您将获得视图对象,然后在当前代码中创建linearlayout对象,并添加viewObject.addView(“linearlayout对象”);。我想,这对你完全有帮助。

也许我不明白。。但我希望有一个基于线性布局索引的解决方案
private void addLevel() {
        LinearLayout mainActivityLayout = (LinearLayout)findViewById(R.id.ly_create_structure);
        LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        LinearLayout ly = (LinearLayout) li.inflate(R.create_structure, null);

        ImageButton ib = (ImageButton) ly.findViewById(R.id.add_level);        

       /**
         * New code to get index
         */
        ViewGroup vg = (ViewGroup) ib.getParent();
        int index = mainActivityLayout.indexOfChild(vg);


        ib.setOnClickListener( new View.OnClickListener() {         
            @Override
            public void onClick(View v) {
                showDialog();

            }
        });

        mainActivityLayout.addView(ly, index);
    }
ViewGroup vg = (ViewGroup) ib.getParent();
int index = mainActivityLayout.indexOfChild(vg);
private void addLevel(int index) {
        LinearLayout mainActivityLayout = (LinearLayout)findViewById(R.id.ly_create_structure);
        LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        LinearLayout ly = (LinearLayout) li.inflate(R.create_structure, null);

        ImageButton ib = (ImageButton) ly.findViewById(R.id.add_level);        

        ib.setOnClickListener( new View.OnClickListener() {         
            @Override
            public void onClick(View v) {
                showDialog();

            }
        });

        mainActivityLayout.addView(ly, index);
    }