Android - 创建一些框架布局并以编程方式将它们与 Gravity 对齐

Android - creating a few frame layout and align them with Gravity programmatically

我有一组结果,我想在不同的 FrameLayout 中显示每个结果。我的 activity 已经包含一个包含 RelativeLayout 的 ScrollView。我正在使用 'for' 创建每个 FrameLayout,但现在每个结果参数都显示在同一位置 - 这可能是因为重力参数设置不正确。

这是我的代码:

         for (int i = 0; i < results.length; ++i) {
                LayoutParams textViewsLayoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT,
                        Gravity.CLIP_VERTICAL);
                LayoutParams dividerLayoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, 1,
                        Gravity.CLIP_VERTICAL);
                LayoutParams frameLayoutParams = new LayoutParams(LayoutParams.MATCH_PARENT,
                        FrameLayout.LayoutParams.WRAP_CONTENT, Gravity.CLIP_VERTICAL);
                frameLayoutParams.setMargins(5, 5, 5, 5);
                //Initializing text views
                TextView domain = new TextView(SinglePhotoResults.this);
                domain.setText(results[i].getDomain());
                domain.setTextSize(22);
                domain.setGravity(Gravity.CLIP_VERTICAL);
                domain.setTextColor(Color.parseColor("#1A1AFF"));
                domain.setLayoutParams(textViewsLayoutParams);

                TextView url = new TextView(SinglePhotoResults.this);
                url.setText(results[i].getURL());
                url.setTextSize(22);
                url.setGravity(Gravity.CLIP_VERTICAL);
                url.setTextColor(Color.parseColor("#00CC00"));
                url.setLayoutParams(textViewsLayoutParams);

                View divider = new View(SinglePhotoResults.this);
                divider.setBackgroundColor(Color.parseColor("#00ff00"));
                divider.setLayoutParams(dividerLayoutParams);

                TextView percents = new TextView(SinglePhotoResults.this);
                Integer percentage = results[i].getPrecents();
                percents.setText(percentage.toString());
                percents.setTextSize(22);
                percents.setGravity(Gravity.CLIP_VERTICAL);
                percents.setTextColor(Color.parseColor("#000000"));
                percents.setLayoutParams(textViewsLayoutParams);

                TextView copiedWords = new TextView(SinglePhotoResults.this);
                Integer copiedWordsNum = results[i].getNumberOfCopiedWords();
                copiedWords.setText(copiedWordsNum.toString());
                copiedWords.setTextSize(22);
                copiedWords.setGravity(Gravity.CLIP_VERTICAL);
                copiedWords.setTextColor(Color.parseColor("#000000"));
                copiedWords.setLayoutParams(textViewsLayoutParams);
                //Initializing frame layout
                FrameLayout frameLayout = new FrameLayout(SinglePhotoResults.this);
                frameLayout.setLayoutParams(frameLayoutParams);
                frameLayout.setBackgroundColor(Color.parseColor("#ffffff"));
                frameLayout.setPadding(5, 5, 5, 5);
                frameLayout.setForegroundGravity(Gravity.CLIP_VERTICAL);
                //Adding views to FrameLayout
                frameLayout.addView(domain);
                frameLayout.addView(url);
                frameLayout.addView(divider);
                frameLayout.addView(percents);
                frameLayout.addView(copiedWords);
                ////Adding frameLayout to relativeLayout
                RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.singlePhotoResultRelativeLayout);
                relativeLayout.addView(frameLayout);
            }

我想让它一个接一个地显示。 如何正确设置 FrameLayouts 及其内部的重力参数?

您应该使用 LinearLayout 而不是使用相对布局。如果您仍想使用相对布局,则需要在下一个元素放置的元素下方指定。 (我建议改用列表视图)。