【问题标题】:Dynamically add Relative Layouts that have the same layout as base动态添加与基础布局相同的相对布局
【发布时间】:2019-07-21 17:32:17
【问题描述】:

如何根据 xml 布局动态生成 RelativeLayout(发布在下面)?相对布局被放置在作为滚动视图子级的线性布局中。

<RelativeLayout
                android:id="@+id/post_0"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingLeft="16dp"
                android:paddingRight="16dp">

                <TextView
                    android:id="@+id/op_username"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentStart="true"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentTop="true"
                    android:layout_marginStart="16dp"
                    android:layout_marginLeft="16dp"
                    android:layout_marginTop="8dp"
                    android:text="Username" />

                <ImageView
                    android:id="@+id/image"
                    android:layout_width="match_parent"
                    android:layout_height="250dp"
                    android:layout_below="@+id/op_username"
                    android:layout_alignParentStart="true"
                    android:layout_alignParentLeft="true"
                    android:layout_marginStart="31dp"
                    android:layout_marginLeft="31dp"
                    android:layout_marginTop="15dp"
                    app:srcCompat="@android:drawable/btn_star_big_on"
                    tools:srcCompat="@android:drawable/btn_star_big_on" />
            </RelativeLayout>

【问题讨论】:

    标签: java android


    【解决方案1】:

    假设您的数据库中有所有图像及其 TextView 的文本,然后创建一个 RecyclerView(如果项目数量相对较大)或一个简单的ListView,然后创建一个 CustomAdapter 类将您的 RelativeLayout 添加到您的主 ListView/ ScrollView/ RecyclerView

    首先要完全实现这一点,您需要有一个 Item 类,它可以在 Object 中存储用户的用户名和图像(请注意,您需要将图像 URI 作为字符串传递)。

    public class Items {
    
        private final int id;
        private final String image;
    
        public Items(int id,String image){
            this.name=name;
            this.image=image;
        }
        //Include all the Getters and Setters here
    }
    

    然后假设您已将所有元素放入ArrayList&lt;Items&gt;,那么您需要实现一个名为 CustomAdaper 的适配器类,它扩展了 ArrayAdapter 并需要为 getView 编写一个函数来获取特定视图来填写您的ScrollView/RecyclerView/ListView

    我找到了一个链接来帮助您实现 CustomAdapter here

    最后在 MainActivity 中,您必须添加几行代码,

    final ListView listView = (ListView) findViewById(R.id.list_view);    //name of list view file
    final ArrayList<Items> arrayList = dbHelper.retrieveFromDB(); // Name of the Database class and the function to retrieve all the elements into an ArrayList in that class
    CustomAdapter adapter = new CustomAdapter(this /* The current Context */, R.layout.list_item, arrayList); // Create an object of custom adapter initialize it with the desired data (in this case the arrayList) and the layout required ( layout mentioned in the question) 
    listView.setAdapter(adapter);
    

    【讨论】:

      【解决方案2】:

      如果它是一个可以包含大量项目的动态列表,请使用 recyclerview 并将该相对布局放置在 cardview 中。 如果动态列表的最大潜在项目数量也很少,您可以添加相对布局并隐藏它们,然后根据需要填充并显示它们,但我建议您倾向于前者。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-06-21
        • 2017-01-03
        • 2012-07-06
        • 2013-03-28
        • 1970-01-01
        • 2013-12-31
        • 1970-01-01
        相关资源
        最近更新 更多