叠加文本在的ImageView中的FrameLayout progrmatically - 机器人 [英] Overlay text over imageview in framelayout progrmatically - Android

查看:164
本文介绍了叠加文本在的ImageView中的FrameLayout progrmatically - 机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个的FrameLayout在布局的中心和底部的一个TextView上的图像,因为它是在这里看到:

http://developer.android.com/resources/articles/布局花招merge.html

 <的FrameLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
 机器人:layout_width =FILL_PARENT
 机器人:layout_height =FILL_PARENT>

< ImageView的
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT

    机器人:scaleType =中心
    机器人:SRC =@可绘制/ golden_gate/>

<的TextView
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_marginBottom =20dip
    机器人:layout_gravity =center_horizo​​ntal |底

    机器人:填充=12dip

    机器人:后台=#AA000000
    机器人:文字颜色=#FFFFFFFF

    机器人:文本=金门/>

< /的FrameLayout>
 

我想以编程方式,但没有运气实现这个..我总是得到的TextView左上方corner..can谁能帮助?这是我的code:

 的FrameLayout frameLay =新的FrameLayout(MainScreen.this);
的LayoutParams layoutParamsFrame =新的LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);

frameLay.setLayoutParams(layoutParamsFrame);

LinearLayout.LayoutParams layoutParamsImage =新LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);

ImageView的ImageView的=新ImageView的(MainScreen.this);
imageView.setImageResource(R.drawable.movi​​e);
imageView.setLayoutParams(layoutParamsImage);

TextView的theText =新的TextView(MainScreen.this);
theText.setText(金门);
theText.setTextColor(Color.WHITE);
theText.setTypeface(Typeface.DEFAULT_BOLD);

的LayoutParams layoutParamsText =新的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 =新的LayoutParams(LayoutParams.FILL__PARENT,
        LayoutParams.FILL_PARENT);
 

当您设置重力TextView的,这意味着你是在告诉TextView的在哪里放置它的孩子。但由于你的TextView只有文字的大小,重力不会显示出任何区别。因此,只要TextView的,以填补父。

但我认为RelativeLayout的是很多更适合这个比的FrameLayout。使用RelativeLayout的,这是它会如何看

  RelativeLayout的rLayout =新RelativeLayout的(这一点);
的LayoutParams rlParams =新的LayoutParams(LayoutParams.FILL_PARENT
        ,LayoutParams.FILL_PARENT);
rLayout.setLayoutParams(rlParams);

ImageView的形象=新ImageView的(这一点);
image.setImageResource(R.drawable.icon);
image.setLayoutParams(rlParams);

RelativeLayout.LayoutParams tParams =新RelativeLayout.LayoutParams
        (LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
tParams.addRule(RelativeLayout.CENTER_HORIZONTAL,RelativeLayout.TRUE);
tParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,RelativeLayout.TRUE);
TextView的文本=新的TextView(本);
text.setText(金门);
text.setTextColor(Color.WHITE);
text.setTypeface(Typeface.DEFAULT_BOLD);
text.setLayoutParams(tParams);

rLayout.addView(图像);
rLayout.addView(文本);
的setContentView(rLayout);
 

I am trying to implement a textview over an image in a framelayout at the center and bottom of the layout as it is seen here:

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>

I am trying to implement this programatically but with no luck.. I always get the textview on the top left corner..can anyone help? Here is my code:

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

解决方案

All you need to do ias make the textview to fill the parent like

LayoutParams layoutParamsText= new LayoutParams(LayoutParams.FILL__PARENT, 
        LayoutParams.FILL_PARENT); 

When you set gravity to the textview, it mean you are telling the textview where to position its children. But since your textview only has the size of your text, the gravity wont show any difference. So just make the textview to fill the parent.

But I think RelativeLayout is a lot more suitable for this than the FrameLayout. Using the RelativeLayout this is how it would look

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

这篇关于叠加文本在的ImageView中的FrameLayout progrmatically - 机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆