2011-10-09 23 views
3

我想實現在佈局的中心和底部的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); 

回答

7

所有你需要做的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); 
+0

它的工作原理!非常感謝你! – andreasv

+0

@blessenm萬分感謝 –

1

而不是一個FrameLayout,您可能想嘗試使用RelativeLayout作爲容器。這樣,您可以將覆蓋文字放在任何需要的位置。你應該爲你工作是一個RelativeLayout的內的下列分配給您的TextView:

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

然後,您可以微調帶空白的位置。

相關問題