简体   繁体   English

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

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

I am not really understanding how I can use Java to create these layouts, edit their attributes, and add views to them. 我不太了解如何使用Java创建这些布局,编辑其属性以及向其中添加视图。

I know I can create a GridLayout object and give it the number of rows and columns with 我知道我可以创建一个GridLayout对象,并为其赋予行数和列数

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

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. 您应该从单个xml文档和父布局开始。 Then you can give that an id and call it dynamically. 然后,您可以为其指定一个ID并动态调用它。 You can add to this layout whatever you want. 您可以根据需要添加到此布局。 Heres some code I had from doing it. 这是我从中获得的一些代码。 My parent linear layout is my_ll: 我的父级线性布局为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. 您还可以动态地将onClickListeners添加到每个视图。 The addView is the part that will add it dynamically. addView是将动态添加它的部分。

You could add your views in any viewGroup by: 您可以在任何viewGroup by中添加视图:

YourViewGroup.addView(yourChildView);

But I think ListView will serve you better because: 但我认为ListView将为您提供更好的服务,因为:

  • You don't have to worry about each view's handling; 您不必担心每个视图的处理方式。
  • You will have automatic scroll; 您将具有自动滚动;
  • You can simply manage your data via Adapter . 您只需通过Adapter即可管理数据。

EDIT: As of most recent APIs, you should use a RecyclerView for various reasons. 编辑:由于最新的API,出于各种原因,您应该使用RecyclerView It works similar to the previous adapters and you can accomplish any list/grid behavior you want. 它的工作方式与以前的适配器相似,您可以完成所需的任何列表/网格行为。

暂无
暂无

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

相关问题 如何在相对布局或任何布局中动态添加视图 - How can I add Views dynamically in a Relative layout or in any Layout 如何以编程方式更改这些视图布局? 我在 xml 文件中有视图,但后来想以编程方式更改它们的大小 - How to change these views layouts programmatically? I have the views in xml file, but later want to change their size programmatically 我如何在线性布局(根视图)中包含两个线性布局(嵌套视图)下方的按钮 position - how can i position a button below two linear layouts(nested views) containted in a linear layout (root view) Android - 使用现有的XML布局动态地向ListView添加项目? - Android - Dynamically add items to ListView using existing XML layouts? android动态添加布局xml中的元素 - android dynamically add element from layout xml 如何动态地将LinearLayoutCompat视图添加到现有xml布局? - How do I add a LinearLayoutCompat view to an existing xml layout dynamically? 当我向布局xml文件添加按钮时,Android应用程序崩溃 - Android app crashes when I add a button to the layout xml file 如何修复我的 android 应用程序中的布局视图? - How can I fix the layout views in my android app? 打开任何android应用程序时如何显示布局(xml文件)? - how i can show my layout(xml file) when i open any android app? Android:我们可以在布局中动态添加recyclerview吗 - Android: Can we add recyclerview dynamically in layout
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM