简体   繁体   English

如何以编程方式在内部添加带有2个视图的几个relativeLayout?

[英]How to add several relativeLayout with 2 views inside programmatically?

I took a look on stackoverflow but I can't find the answer. 我看了一下stackoverflow,但找不到答案。 I would like to add several rows ( RelativeLayout ) into the parent ( LinearLayout ). 我想在父级( LinearLayout )中添加几行( RelativeLayout )。 The RelativeLayout would be composed of 2 views, a ImageView on the left and a TextView on its right, both into the same row, for each item: RelativeLayout将由2个视图组成,左侧为ImageView ,右侧为TextView ,每个视图都位于同一行中:

LinearLayout userLayout = (LinearLayout) view.findViewById(R.id.participant_user);
RelativeLayout rL = new RelativeLayout(context);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_LEFT)

for (Participant participant : participants) {
    TextView textView = setTextView(context, participant.getName());
    rL.addView(textView, params);
    ImageView imageView = new ImageView(context);
    imageView.setImageResource(R.drawable.bz_ic_default_user);
    rL.addView(imageView, params);
    userLayout.addView(rL);
}

It doesn't work, the first element is only displayed or the app crashed... 它不起作用,仅显示第一个元素,或者应用崩溃了...

Thank you for your help ! 谢谢您的帮助 !

You should probably use a ListAdapater or RecylerView here 您可能应该在这里使用ListAdapater或RecylerView

You are trying to create a list of items with an unknown #. 您正在尝试创建具有未知编号的项目列表。 The way you are doing it, performance will be bad and you are inflating views into a linearlayout and not reusing views. 这样的做法会降低性能,并且会使视图膨胀为线性布局,而不会重用视图。

You are creating a RelativeLayout, filling it n times (one per participant) and trying to add it n times. 您正在创建一个RelativeLayout,将其填充n次(每个参与者一次),然后尝试将其添加n次。 At the very least you should create n different RelativeLayouts. 至少您应该创建n个不同的RelativeLayouts。

Anyway, as a rule of thumb, every time you have to crete a list ask yourself: will it contain 10 elements or 1000. If it's 10, then you can follow your approach (but I would advise to create every row inflating an xml instead od instantiating the views), if it's 1000 use a ListView (or some kind of RecyclerView), much simpler and efficient. 无论如何,根据经验,每次创建列表时,都要问自己:它包含10个元素还是1000个元素。如果是10个元素,则可以按照您的方法进行操作(但是我建议创建每行以xml填充的方式od实例化视图),如果它是1000,则使用ListView(或某种RecyclerView),会更简单,更有效。

As the previous answers stated, using a ListAdapter would probably make things work. 如先前的回答所述,使用ListAdapter可能会使事情工作。 Here is an example . 这是一个例子

If you really want to do it your way, you should share your error log (LogCat output). 如果您确实希望按照自己的方式进行操作,则应该共享错误日志(LogCat输出)。

As everyone here said, It's better to use the RecyclerView or the ListView . 正如这里的所有人所说,最好使用RecyclerViewListView But if you want do it in this way for another reason, then I think that the best that you can do is: 但是,如果您出于其他原因想要以这种方式进行操作,那么我认为您可以做的最好的事情是:

        LinearLayout mainLayout = new LinearLayout(this);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        mainLayout.setLayoutParams(params);
        LayoutInflater inflater = LayoutInflater.from(this);
        for (Participant participant : participants) {
            View rowView = inflater.inflate(R.layout.row, mainLayout, false);
            ((TextView) rowView.findViewById(R.id.name)).setText(participant.name);
            ((ImageView) rowView.findViewById(R.id.photo)).setImageResource(R.drawable.photo); //BTW You can set it in the xml and avoid this
            mainLayout.addView(rowView);
        }
        setContentView(mainLayout);

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

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