android - 在 FrameLayout 中的相对布局中创建和添加动态 View

标签 android android-layout layoutparams

我做的是这样的

<Dynamic Framelayout >
    <Dynamic Relaive Layout>

       <Dynamic imageView>
       <Dynamic TextView>

    </Dynamic Relaive Layout>
</Dynamic Framelayout>

我已经按照上面的讨论创建了布局并放置了 View 。

但是现在我想textview应该放在image view下面

这是我得到的。

enter image description here

注意红色 TextView 。这应该在 ImageView 下方并水平居中。

这是我的代码。

final FrameLayout main = (FrameLayout) findViewById(R.id.add_dynamic_views_frame);
int numViews = drawbbleIds.length;
for (int i = 0; i < numViews; i++) {

    RelativeLayout rel_inner = new RelativeLayout(SplashSearch.this);

    rel_inner.setGravity(Gravity.CENTER_HORIZONTAL);

    ImageView imgView = new ImageView(SplashSearch.this);

    TextView textView = new TextView(SplashSearch.this);

    imgView.setImageDrawable(getResources().getDrawable(drawbbleIds[i]));

    imgView.setTag(i);
    imgView.setId(i);
    imgView.setOnClickListener(SplashSearch.this);

    rel_inner.addView(imgView);

    RelativeLayout.LayoutParams imgLp = (RelativeLayout.LayoutParams) imgView
            .getLayoutParams();

    textView.setTag(i);
    textView.setId(i);

    textView.setText(getResources().getStringArray(R.array.tabs_names)[i]);

    textView.setTextColor(Color.RED);

    rel_inner.addView(textView);

    RelativeLayout.LayoutParams txtLp = (RelativeLayout.LayoutParams) textView
            .getLayoutParams();

    imgLp.addRule(RelativeLayout.BELOW, textView.getId());

    textView.setLayoutParams(imgLp);

    FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(70, 70);

    // Place all views in the center of the
    // layout. We'll transform them
    // away from there in the code below.
    lp.gravity = Gravity.CENTER;

    // Set layout params on view.
    rel_inner.setLayoutParams(lp);

    // Calculate the angle of the current
    // view.
    // Adjust by 90 degrees to
    // get View 0 at the top. We need the
    // angle
    // in degrees and radians.
    float angleDeg = i * 360.0f / numViews - 90.0f;
    float angleRad = (float) (angleDeg * Math.PI / 180.0f);

    rel_inner.setTranslationX(250 * (float) Math.cos(angleRad));
    rel_inner.setTranslationY(250 * (float) Math.sin(angleRad));

    main.addView(rel_inner);
}

最佳答案

尝试使用这段代码:

 final FrameLayout main = (FrameLayout) findViewById(R.id.frame);
            int numViews = 5;
            for (int i = 0; i < numViews; i++) {

                RelativeLayout rel_inner = new RelativeLayout(this);

                rel_inner.setGravity(Gravity.CENTER_HORIZONTAL);

                ImageView imgView = new ImageView(this);

                imgView.setImageDrawable(getResources().getDrawable(
                        R.drawable.ic_launcher));

                imgView.setTag(i);
                imgView.setId(i + 1);
                // imgView.setOnClickListener(SplashSearch.this);

                rel_inner.addView(imgView);

                RelativeLayout.LayoutParams imgLp = new RelativeLayout.LayoutParams(
                        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

                TextView textView = new TextView(this);
                textView.setTag(i);
                textView.setId(i);
                textView.setText("" + i);

                textView.setTextColor(Color.RED);
                imgLp.addRule(RelativeLayout.BELOW, imgView.getId());
imgLp.addRule(RelativeLayout.CENTER_HORIZONTAL);
                textView.setLayoutParams(imgLp);

                rel_inner.addView(textView);

                FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(70,
                        LayoutParams.WRAP_CONTENT);

                // Place all views in the center of the
                // layout. We'll transform them
                // away from there in the code below.
                lp.gravity = Gravity.CENTER;

                // Set layout params on view.
                rel_inner.setLayoutParams(lp);

                // Calculate the angle of the current
                // view.
                // Adjust by 90 degrees to
                // get View 0 at the top. We need the
                // angle
                // in degrees and radians.
                float angleDeg = i * 360.0f / numViews - 90.0f;
                float angleRad = (float) (angleDeg * Math.PI / 180.0f);

                rel_inner.setTranslationX(250 * (float) Math.cos(angleRad));
                rel_inner.setTranslationY(250 * (float) Math.sin(angleRad));

                main.addView(rel_inner);
            }

效果很好,希望对你有帮助。

关于android - 在 FrameLayout 中的相对布局中创建和添加动态 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23493504/

相关文章:

android - 两次设置LayoutParams,没有效果?

java - 以编程方式将规则添加到布局中已有的 View - Android

android - Android 市场中的编程 IDE

Android 状态栏与协调器布局一起向上滚动,使状态图标与工具栏标题重叠

android - 如何在android中以编程方式查找TextView的文本区域(高度/宽度)

android - 如何在 android 的表格布局中动态合并一个单元格中的行?

android - 在 ListView 中动态添加 View (在本例中为线性布局)时出现图形错误

java - android studio中的顺序动画,循环

android - 捕获滑动以消除 ViewPager 中的 ListView 手势

java - Android 8.1 带通知 : Use of stream types is deprecated