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

标签 java android gravity

我有一组结果,我想在不同的 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,而不是使用相对布局。如果您仍然想使用相对布局,则需要指定将下一个元素放置在哪个元素下方。 (我建议改为使用 ListView )。

关于java - Android - 创建一些框架布局并以编程方式将它们与重力对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33532714/

相关文章:

android - 如何在Android中为TextInputLayout的提示和文本设置不同的引力

java - Java Web 应用程序中的嵌入式 Derby

java - Cordova 到 Android studio 错误 malformed\uxxxx 编码

java - 如何使我的库、模块或 jar 文件在 diff 项目之间保持同步

android - 使用导航 View 和工具栏不显示后退按钮

android - 在任何textview android中将Toast重力设置为左上角

java - 幻灯片动画循环 Android

eclipse - Android/Eclipse 问题

android - 如何在 Android 中更改 EditTextPreference 的 dialogMessage 的字体?

android - 在 Android 中创建游戏引力(续)?