简体   繁体   中英

How to add a LinearLayout after another LinearLayout?

I created this layout:

<!-- 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>

and this other

<!-- add_level_in_structure.xml -->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ly_level"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/txt_num"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1#" 
        android:layout_weight="1"/>

    <EditText
        android:id="@+id/edit_sb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="number" 
        android:layout_weight="3">
        <requestFocus />
    </EditText>

    <ImageButton
        android:id="@+id/add_level"
        android:scaleType="fitXY" 
        android:layout_width="48dip"
        android:layout_height="48dip"
        android:cropToPadding="false"
        android:paddingLeft="10dp"
        android:src="@drawable/ic_menu_add" />
 </LinearLayout>

I wanto to add programmatically the LinearLayout.ly_level in LinearLayout.ly_create_structure after the click "Add level" in alertDialog (each level in LinearLayout.ly_level has a link to show an alertDialog).

The code is:

public class CreateStructureActivity extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.create_structure);  

        addLevel();
    }


    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);        

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

            }
        });

        mainActivityLayout.addView(ly);
    }



    private void showDialog() {
        final CharSequence[] items = {"Add Level", "Delete"};
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Actions");

        builder.setItems(items, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {

                switch(item){
                    case 0:
                        addLivello();
                    break;

                    case 1:
                        delete();
                    break;
             }
                }
            });

        AlertDialog alert = builder.create();
        alert.show();
    }    
}

With this code I can to add a LinearLayout as the last, but I want to add it after the LinearLayout that contains the button clicked to show an AlertDialog. Could you help me to implement this?

UPDATE 1: I can't understand how to get the index. I trying in this way:

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);
    }

UPDATE 2: I solved the problem. I moving the code below in showDialog()

ViewGroup vg = (ViewGroup) ib.getParent();
int index = mainActivityLayout.indexOfChild(vg);

You can use method at Dev Android

which takes index of view in parameter as well.

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);
    }

Good, First of all, you will get the view object and then create linearlayout object as you are make in the our current code and add viewObject.addView("linearlayout object");. I think, this is help full to you..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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