叠加文本在中的FrameLayout imageview的程序 - Android电子
问题描述:
我想实现在布局的中心和底部的FrameLayout一个TextView过的图像,因为它是在这里看到:叠加文本在中的FrameLayout imageview的程序 - Android电子
http://developer.android.com/resources/articles/layout-tricks-merge.html
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="center"
android:src="@drawable/golden_gate" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dip"
android:layout_gravity="center_horizontal|bottom"
android:padding="12dip"
android:background="#AA000000"
android:textColor="#ffffffff"
android:text="Golden Gate" />
</FrameLayout>
我试图以编程的方式实现这个,但没有运气..我总是得到左上角的textview ..任何人都可以帮忙吗?这里是我的代码:
FrameLayout frameLay = new FrameLayout(MainScreen.this);
LayoutParams layoutParamsFrame = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
frameLay.setLayoutParams(layoutParamsFrame);
LinearLayout.LayoutParams layoutParamsImage= new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
ImageView imageView= new ImageView(MainScreen.this);
imageView.setImageResource(R.drawable.movie);
imageView.setLayoutParams(layoutParamsImage);
TextView theText=new TextView(MainScreen.this);
theText.setText("GOLDEN Gate");
theText.setTextColor(Color.WHITE);
theText.setTypeface(Typeface.DEFAULT_BOLD);
LayoutParams layoutParamsText= new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
theText.setLayoutParams(layoutParamsText);
theText.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.BOTTOM);
frameLay.addView(theText);
frameLay.addView(imageView);
答
所有你需要做的IAS做出的TextView来填补像
LayoutParams layoutParamsText= new LayoutParams(LayoutParams.FILL__PARENT,
LayoutParams.FILL_PARENT);
父当您设置重力TextView的,这意味着你告诉的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);
答
而不是一个FrameLayout,您可能想尝试使用RelativeLayout作为容器。这样,您可以将覆盖文字放在任何需要的位置。你应该为你工作是一个RelativeLayout的内的下列分配给您的TextView:
android:layout_alignParentBottom="true" and android:layout_centerInParent="true"
然后,您可以微调带空白的位置。
它的工作原理!非常感谢你! – andreasv
@blessenm万分感谢 –