Android 在FrameLayout内部的相对布局中创建和添加动态视图

Android 在FrameLayout内部的相对布局中创建和添加动态视图,android,android-layout,layoutparams,Android,Android Layout,Layoutparams,我现在做的是这样的 <Dynamic Framelayout > <Dynamic Relaive Layout> <Dynamic imageView> <Dynamic TextView> </Dynamic Relaive Layout> </Dynamic Framelayout> 我已经创建了布局并放置了上面讨论过的视图 但是现在我想把文本视图放在图像视图下面

我现在做的是这样的

<Dynamic Framelayout >
    <Dynamic Relaive Layout>

       <Dynamic imageView>
       <Dynamic TextView>

    </Dynamic Relaive Layout>
</Dynamic Framelayout>

我已经创建了布局并放置了上面讨论过的视图

但是现在我想把文本视图放在图像视图下面

这是我得到的

注意红色的文本视图。这应该在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.add\u dynamic\u views\u frame);
int numViews=drawbbleIds.length;
for(int i=0;i
将视图添加到RelativeLayout后,您正在设置布局参数

textView.setLayoutParams(imgLp);
rel_inner.addView(imgView);
rel_inner.addView(textView);
首先如上所述设置LayoutParams,然后将视图添加到RelativeLayout

textView.setLayoutParams(imgLp);
rel_inner.addView(imgView);
rel_inner.addView(textView);
编辑:尝试复制粘贴此代码。我无法测试,因为我没有资源、文件等,但这应该可以做到

    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);

        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);

        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);

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

        imgLp.addRule(RelativeLayout.CENTER_HORIZONTAL);
        imgView.setLayoutParams(imgLp);

        rel_inner.addView(imgView);

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

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

        textView.setTextColor(Color.RED);

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

        txtLp.addRule(RelativeLayout.CENTER_HORIZONTAL);
        txtLp.addRule(RelativeLayout.BELOW, imgView.getId());
        textView.setGravity(Gravity.CENTER);
        textView.setLayoutParams(txtLp);

        rel_inner.addView(textView);

        // 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.add\u dynamic\u views\u frame);
int numViews=drawbbleIds.length;
for(int i=0;i
在此处使用
线性布局
而不是
相对布局。

尝试使用以下代码:

 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);
            }
final FrameLayout main=(FrameLayout)findviewbyd(R.id.frame);
int numViews=5;
for(int i=0;i