繁体   English   中英

如何动态将布局和视图添加到Android XML布局文件?

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

我正在创建一个需要动态生成XML文件的应用程序。 布局开始时基本上是空的(不是线性布局),然后循环遍历JSON数组并为数组中的每个元素创建下面显示的XML代码(数组中每个元素的唯一不同是视图的ID) )。

我不太了解如何使用Java创建这些布局,编辑其属性以及向其中添加视图。

我知道我可以创建一个GridLayout对象,并为其赋予行数和列数

GridLayout doc = new GridLayout(3, 3);

但是我不知道如何编辑所有特定属性,然后在布局内部添加视图。

通过Jave代码创建布局,编辑其属性以及在该布局中添加视图的最佳方法是什么?

谢谢。

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

您应该从单个xml文档和父布局开始。 然后,您可以为其指定一个ID并动态调用它。 您可以根据需要添加到此布局。 这是我从中获得的一些代码。 我的父级线性布局为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);

您还可以动态地将onClickListeners添加到每个视图。 addView是将动态添加它的部分。

您可以在任何viewGroup by中添加视图:

YourViewGroup.addView(yourChildView);

但我认为ListView将为您提供更好的服务,因为:

  • 您不必担心每个视图的处理方式。
  • 您将具有自动滚动;
  • 您只需通过Adapter即可管理数据。

编辑:由于最新的API,出于各种原因,您应该使用RecyclerView 它的工作方式与以前的适配器相似,您可以完成所需的任何列表/网格行为。

暂无
暂无

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

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