以编程方式将文本覆盖在framelayout中的imageview上-Android
我试图在布局中心和底部的framelayout中的图像上实现文本视图,如图所示:以编程方式将文本覆盖在framelayout中的imageview上-Android,android,textview,imageview,android-framelayout,Android,Textview,Imageview,Android Framelayout,我试图在布局中心和底部的framelayout中的图像上实现文本视图,如图所示: 你所需要做的就是让textview像这样填充父视图 LayoutParams layoutParamsText= new LayoutParams(LayoutParams.FILL__PARENT, LayoutParams.FILL_PARENT); 当您将重力设置为textview时,意味着您正在告诉textview其子对象的位置。但是由于文本视图只有文本的大小,因此重力不会显示任何差
你所需要做的就是让textview像这样填充父视图
LayoutParams layoutParamsText= new LayoutParams(LayoutParams.FILL__PARENT,
LayoutParams.FILL_PARENT);
当您将重力设置为textview时,意味着您正在告诉textview其子对象的位置。但是由于文本视图只有文本的大小,因此重力不会显示任何差异。因此,只需创建textview来填充父对象
但我认为RelativeLayout比FrameLayout更适合于此。使用RelativeLayout这就是它的外观
RelativeLayout rLayout = new RelativeLayout(this);
LayoutParams rlParams = new LayoutParams(LayoutParams.FILL_PARENT
,LayoutParams.FILL_PARENT);
rLayout.setLayoutParams(rlParams);
ImageView image= new ImageView(this);
image.setImageResource(R.drawable.icon);
image.setLayoutParams(rlParams);
RelativeLayout.LayoutParams tParams = new RelativeLayout.LayoutParams
(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
tParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
tParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
TextView text=new TextView(this);
text.setText("GOLDEN Gate");
text.setTextColor(Color.WHITE);
text.setTypeface(Typeface.DEFAULT_BOLD);
text.setLayoutParams(tParams);
rLayout.addView(image);
rLayout.addView(text);
setContentView(rLayout);
您可能希望尝试使用RelativeLayout作为容器,而不是FrameLayout。这样,您就可以将覆盖文本放置在任何需要的位置。您应该在RelativeLayout中将以下内容分配给您的TextView:
android:layout_alignParentBottom="true" and android:layout_centerInParent="true"
然后可以使用边距微调放置
RelativeLayout rLayout = new RelativeLayout(this);
LayoutParams rlParams = new LayoutParams(LayoutParams.FILL_PARENT
,LayoutParams.FILL_PARENT);
rLayout.setLayoutParams(rlParams);
ImageView image= new ImageView(this);
image.setImageResource(R.drawable.icon);
image.setLayoutParams(rlParams);
RelativeLayout.LayoutParams tParams = new RelativeLayout.LayoutParams
(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
tParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
tParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
TextView text=new TextView(this);
text.setText("GOLDEN Gate");
text.setTextColor(Color.WHITE);
text.setTypeface(Typeface.DEFAULT_BOLD);
text.setLayoutParams(tParams);
rLayout.addView(image);
rLayout.addView(text);
setContentView(rLayout);
android:layout_alignParentBottom="true" and android:layout_centerInParent="true"