以编程方式在帧布局中的ImageView上叠加文本- Android

4
我正在尝试在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);
2个回答

7
您只需要将TextView填充到父容器中,如下所示:
LayoutParams layoutParamsText= new LayoutParams(LayoutParams.FILL__PARENT, 
        LayoutParams.FILL_PARENT); 

当你设置文本视图的重力时,意味着你告诉文本视图在哪里定位其子元素。但由于你的文本视图仅具有文本大小,因此重力不会显示任何差异。所以只需使文本视图填充父级即可。

但我认为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);

1

如果你不想使用FrameLayout,可以尝试使用RelativeLayout作为容器。这样,你就可以将覆盖文本放置在任何你想要的位置。对于你来说应该有效的方法是在RelativeLayout中为你的TextView分配以下内容:

android:layout_alignParentBottom="true" and android:layout_centerInParent="true"

然后您可以通过边距微调位置。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接