简体   繁体   中英

How can I dynamically add layouts and views to an Android XML layout file?

I am creating an app that needs to dynamically generate an XML file. The layout starts essentially empty (other than a linear layout), then I loop through a JSON array and create the XML code shown below for EACH element in the array (the only thing different for each element in the array is the ids for the views).

I am not really understanding how I can use Java to create these layouts, edit their attributes, and add views to them.

I know I can create a GridLayout object and give it the number of rows and columns with

GridLayout doc = new GridLayout(3, 3);

But I can't figure out how to edit all of the specific attributes, and then add views inside of the layout.

What is the best way to create a layout, edit it's attributes, and add views within that layout through Jave code?

Thank you.

<GridLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:columnCount="3"
    android:rowCount="3"
    android:layout_marginBottom="10dp"
    android:background="#3c37ff00"
    android:id="@+id/doctor1"
    android:longClickable="true">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Dr. Sam"
        android:id="@+id/doctor1_name"
        android:layout_row="0"
        android:layout_column="0"
        android:textStyle="bold"
        android:textSize="26dp"
        android:typeface="sans" />

    <ImageButton
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/doctor1_profile"
        android:layout_row="0"
        android:layout_column="1"
        android:src="@drawable/no_pic"
        android:scaleType="fitXY"
        android:layout_rowSpan="3"
        android:layout_columnSpan="1"
        android:layout_gravity="right"
        android:background="#00ffffff" />

    <ImageButton
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/doctor1_action"
        android:layout_row="0"
        android:layout_column="2"
        android:src="@drawable/abc_ic_menu_paste_mtrl_am_alpha"
        android:scaleType="centerInside"
        android:layout_rowSpan="3"
        android:layout_columnSpan="1"
        android:background="#47ffffff"
        android:clickable="true"
        android:onClick="setContentView" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Very Close"
        android:id="@+id/doctor1_distance"
        android:layout_row="1"
        android:layout_column="0"
        android:textStyle="bold" />

</GridLayout>

You should start with a single xml document and a parent layout. Then you can give that an id and call it dynamically. You can add to this layout whatever you want. Heres some code I had from doing it. My parent linear layout is my_ll:

LinearLayout ll = (LinearLayout) myInflatedView.findViewById(R.id.my_ll);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            params.gravity = Gravity.CENTER;

            // Use this for each image in the list
            final ImageView image = new ImageView(getActivity());
            image.setAdjustViewBounds(true);            // Scales it to the screen
            image.setId(i);                             // Sets each with unique id
            ll.addView(image, params);                  // Adds the ImageView to screen BEFORE adding image (important)

            Picasso.with(getActivity())                 // THEN you add the image from photosList
                    .load(photosList.get(i))
                    .into(image);

You can add onClickListeners to each view also dynamically. The addView is the part that will add it dynamically.

You could add your views in any viewGroup by:

YourViewGroup.addView(yourChildView);

But I think ListView will serve you better because:

  • You don't have to worry about each view's handling;
  • You will have automatic scroll;
  • You can simply manage your data via Adapter .

EDIT: As of most recent APIs, you should use a RecyclerView for various reasons. It works similar to the previous adapters and you can accomplish any list/grid behavior you want.

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