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

编程入门 行业动态 更新时间:2024-10-22 10:53:06
本文介绍了在FrameLayout的相对布局中创建和添加动态视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在做的事情是这样的

What I'm doing is something like this

<Dynamic Framelayout > <Dynamic Relaive Layout> <Dynamic imageView> <Dynamic TextView> </Dynamic Relaive Layout> </Dynamic Framelayout>

如上所述,我已经创建了布局并放置了视图.

I have created the layout and placed the views as I discussed above.

但是现在我想将textview放置在图像视图下方

But Now I want to textview should be placed below the image view

这就是我要得到的.

注意红色文本视图.它应该在imageview下方并水平居中.

notice the RED textviews. this should be below the imageview and horizontally centered.

这是我的代码.

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

效果很好.希望对您有帮助.

It works well.Hope it will help you.

更多推荐

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

本文发布于:2023-11-11 18:11:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1579195.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:建和   视图   布局   中创   动态

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!