以编程方式在framelayout中覆盖imageview上的文本 – Android
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了以编程方式在framelayout中覆盖imageview上的文本 – Android,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2989字,纯文字阅读大概需要5分钟。
内容图文
![以编程方式在framelayout中覆盖imageview上的文本 – Android](/upload/InfoBanner/zyjiaocheng/808/41d3f6b67beb4ba88795a9c593b10089.jpg)
我正在尝试在布局的中心和底部的framelayout中的图像上实现textview,如下所示:
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);
解决方法:
所有你需要做的就是让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中覆盖imageview上的文本 – Android全部内容,希望文章能够帮你解决以编程方式在framelayout中覆盖imageview上的文本 – Android所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。